From af694b97cbcab522d04fd4a8820637847d1a696e Mon Sep 17 00:00:00 2001 From: Jon Meow Date: Fri, 6 May 2022 15:30:25 -0700 Subject: [PATCH] Prefix most macro names with CARBON_ (#1232) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I'm doing this to avoid macro name conflicts, following https://google.github.io/styleguide/cppguide.html#Preprocessor_Macros: "If you do export a macro from a header, it must have a globally unique name. To achieve this, it must be named with a prefix consisting of your project's namespace name (but upper case)." Commands run: ``` sed -i 's/\(DCHECK\|CHECK\|FATAL\|MAKE_UNIQUE_NAME\|MAKE_UNIQUE_NAME_IMPL\|RAW_EXITING_STREAM\|RETURN_IF_ERROR\|RETURN_IF_ERROR_IMPL\|ASSIGN_OR_RETURN\|ASSIGN_OR_RETURN_IMPL\|DIAGNOSTIC_KIND\|RETURN_IF_STACK_LIMITED\)(/CARBON_\1(/g' $(git ls-files *.cpp *.h *.lpp *.ypp *.def ':!third_party') sed -i 's/#undef DIAGNOSTIC_KIND/#undef CARBON_DIAGNOSTIC_KIND/' toolchain/diagnostics/diagnostic_registry.def ``` Note this isn't *quite* everything, but it's intended to be a large pass at everything: ``` ╚╡git grep '#define ' *.cpp *.h *.lpp *.ypp *.def ':!third_party' | grep -v '#define CARBON' | grep -v _H_ explorer/syntax/lexer.lpp: #define YY_USER_ACTION \ explorer/syntax/lexer.lpp: #define SIMPLE_TOKEN(name) \ explorer/syntax/lexer.lpp: #define ARG_TOKEN(name, arg) \ explorer/syntax/parse_and_lex_context.h:#define YY_DECL \ migrate_cpp/cpp_refactoring/var_decl.cpp:#define ABSTRACT_TYPE(Class, Base) migrate_cpp/cpp_refactoring/var_decl.cpp:#define TYPE(Class, Base) \ ``` We may in particular want to do a pass to clean up #ifdef guards and make them be CARBON_ rooted. --- common/check.h | 25 +- common/check_test.cpp | 14 +- common/error.h | 35 +- common/error_test.cpp | 18 +- common/string_helpers.cpp | 2 +- explorer/ast/declaration.cpp | 2 +- explorer/ast/declaration.h | 4 +- explorer/ast/expression.cpp | 3 +- explorer/ast/expression.h | 15 +- explorer/ast/impl_binding.h | 2 +- explorer/ast/pattern.h | 14 +- explorer/ast/return_term.h | 4 +- explorer/ast/statement.h | 8 +- explorer/ast/static_scope.cpp | 8 +- explorer/fuzzing/ast_to_proto.cpp | 2 +- explorer/fuzzing/fuzzer_util.cpp | 2 +- explorer/fuzzing/fuzzverter.cpp | 3 +- explorer/interpreter/action.cpp | 14 +- explorer/interpreter/action.h | 4 +- explorer/interpreter/action_stack.cpp | 16 +- explorer/interpreter/exec_program.cpp | 6 +- explorer/interpreter/heap.cpp | 14 +- explorer/interpreter/impl_scope.cpp | 12 +- explorer/interpreter/interpreter.cpp | 247 ++++---- explorer/interpreter/resolve_control_flow.cpp | 27 +- explorer/interpreter/resolve_names.cpp | 184 +++--- explorer/interpreter/stack.h | 8 +- explorer/interpreter/type_checker.cpp | 574 ++++++++++-------- explorer/interpreter/value.cpp | 44 +- explorer/interpreter/value.h | 6 +- explorer/main.cpp | 6 +- explorer/syntax/bison_wrap.h | 2 +- explorer/syntax/lexer.lpp | 2 +- explorer/syntax/parse.cpp | 2 +- explorer/syntax/parser.ypp | 4 +- explorer/syntax/prelude.cpp | 4 +- toolchain/diagnostics/diagnostic_kind.cpp | 2 +- toolchain/diagnostics/diagnostic_kind.h | 2 +- toolchain/diagnostics/diagnostic_registry.def | 92 +-- toolchain/lexer/numeric_literal.cpp | 5 +- toolchain/lexer/numeric_literal_benchmark.cpp | 8 +- toolchain/lexer/numeric_literal_test.cpp | 2 +- toolchain/lexer/string_literal.cpp | 4 +- toolchain/lexer/string_literal_fuzzer.cpp | 2 +- toolchain/lexer/string_literal_test.cpp | 2 +- toolchain/lexer/test_helpers.h | 2 +- toolchain/lexer/token_kind.cpp | 4 +- toolchain/lexer/tokenized_buffer.cpp | 43 +- toolchain/lexer/tokenized_buffer_fuzzer.cpp | 8 +- .../lexer/tokenized_buffer_test_helpers.h | 4 +- toolchain/parser/parse_test_helpers.h | 8 +- toolchain/parser/parse_tree.cpp | 16 +- toolchain/parser/parse_tree_fuzzer.cpp | 3 +- toolchain/parser/parser_impl.cpp | 82 +-- toolchain/parser/precedence.cpp | 10 +- toolchain/semantics/semantics_ir_factory.cpp | 8 +- toolchain/source/source_buffer.cpp | 4 +- 57 files changed, 869 insertions(+), 779 deletions(-) diff --git a/common/check.h b/common/check.h index 531ca3a842b4..d30e0b6ccc9e 100644 --- a/common/check.h +++ b/common/check.h @@ -12,7 +12,7 @@ namespace Carbon { // Raw exiting stream. This should be used when building other forms of exiting // macros like those below. It evaluates to a temporary `ExitingStream` object // that can be manipulated, streamed into, and then will exit the program. -#define RAW_EXITING_STREAM() \ +#define CARBON_RAW_EXITING_STREAM() \ Carbon::Internal::ExitingStream::Helper() | Carbon::Internal::ExitingStream() // Checks the given condition, and if it's false, prints a stack, streams the @@ -20,29 +20,30 @@ namespace Carbon { // a bug in the application. // // For example: -// CHECK(is_valid) << "Data is not valid!"; -#define CHECK(condition) \ +// CARBON_CHECK(is_valid) << "Data is not valid!"; +#define CARBON_CHECK(condition) \ (condition) ? (void)0 \ - : RAW_EXITING_STREAM() \ + : CARBON_RAW_EXITING_STREAM() \ << "CHECK failure at " << __FILE__ << ":" << __LINE__ \ << ": " #condition \ << Carbon::Internal::ExitingStream::AddSeparator() // DCHECK calls CHECK in debug mode, and does nothing otherwise. #ifndef NDEBUG -#define DCHECK(condition) CHECK(condition) +#define CARBON_DCHECK(condition) CARBON_CHECK(condition) #else -#define DCHECK(condition) CHECK(true || (condition)) +#define CARBON_DCHECK(condition) CARBON_CHECK(true || (condition)) #endif -// This is similar to CHECK, but is unconditional. Writing FATAL() is clearer -// than CHECK(false) because it avoids confusion about control flow. +// This is similar to CHECK, but is unconditional. Writing CARBON_FATAL() is +// clearer than CARBON_CHECK(false) because it avoids confusion about control +// flow. // // For example: -// FATAL() << "Unreachable!"; -#define FATAL() \ - RAW_EXITING_STREAM() << "FATAL failure at " << __FILE__ << ":" << __LINE__ \ - << ": " +// CARBON_FATAL() << "Unreachable!"; +#define CARBON_FATAL() \ + CARBON_RAW_EXITING_STREAM() \ + << "FATAL failure at " << __FILE__ << ":" << __LINE__ << ": " } // namespace Carbon diff --git a/common/check_test.cpp b/common/check_test.cpp index 4b88baa54402..ed2b824e8f0b 100644 --- a/common/check_test.cpp +++ b/common/check_test.cpp @@ -9,11 +9,11 @@ namespace Carbon::Testing { namespace { -TEST(CheckTest, CheckTrue) { CHECK(true); } +TEST(CheckTest, CheckTrue) { CARBON_CHECK(true); } TEST(CheckTest, CheckFalse) { // TODO: figure out why we can't use \\d+ instead of .+ in these patterns. - ASSERT_DEATH({ CHECK(false); }, + ASSERT_DEATH({ CARBON_CHECK(false); }, "Stack trace:\n" ".+\n" "CHECK failure at common/check_test.cpp:.+: false\n"); @@ -25,12 +25,12 @@ TEST(CheckTest, CheckTrueCallbackNotUsed) { called = true; return "called"; }; - CHECK(true) << callback(); + CARBON_CHECK(true) << callback(); EXPECT_FALSE(called); } TEST(CheckTest, CheckFalseMessage) { - ASSERT_DEATH({ CHECK(false) << "msg"; }, + ASSERT_DEATH({ CARBON_CHECK(false) << "msg"; }, "CHECK failure at common/check_test.cpp:.+: false: msg\n"); } @@ -38,15 +38,15 @@ TEST(CheckTest, CheckOutputForms) { const char msg[] = "msg"; std::string str = "str"; int i = 1; - CHECK(true) << msg << str << i << 0; + CARBON_CHECK(true) << msg << str << i << 0; } TEST(CheckTest, Fatal) { - ASSERT_DEATH({ FATAL() << "msg"; }, + ASSERT_DEATH({ CARBON_FATAL() << "msg"; }, "FATAL failure at common/check_test.cpp:.+: msg\n"); } -auto FatalNoReturnRequired() -> int { FATAL() << "msg"; } +auto FatalNoReturnRequired() -> int { CARBON_FATAL() << "msg"; } TEST(ErrorTest, FatalNoReturnRequired) { ASSERT_DEATH({ FatalNoReturnRequired(); }, diff --git a/common/error.h b/common/error.h index 5dea8ecb7b23..6d4012c377f9 100644 --- a/common/error.h +++ b/common/error.h @@ -23,7 +23,7 @@ class [[nodiscard]] Error { public: // Represents an error state. explicit Error(llvm::Twine message) : message_(message.str()) { - CHECK(!message_.empty()) << "Errors must have a message."; + CARBON_CHECK(!message_.empty()) << "Errors must have a message."; } Error(Error&& other) noexcept : message_(std::move(other.message_)) {} @@ -63,39 +63,39 @@ class [[nodiscard]] ErrorOr { // Returns the contained error. // REQUIRES: `ok()` is false. auto error() const& -> const Error& { - CHECK(!ok()); + CARBON_CHECK(!ok()); return std::get(val_); } auto error() && -> Error { - CHECK(!ok()); + CARBON_CHECK(!ok()); return std::get(std::move(val_)); } // Returns the contained value. // REQUIRES: `ok()` is true. auto operator*() -> T& { - CHECK(ok()); + CARBON_CHECK(ok()); return std::get(val_); } // Returns the contained value. // REQUIRES: `ok()` is true. auto operator*() const -> const T& { - CHECK(ok()); + CARBON_CHECK(ok()); return std::get(val_); } // Returns the contained value. // REQUIRES: `ok()` is true. auto operator->() -> T* { - CHECK(ok()); + CARBON_CHECK(ok()); return &std::get(val_); } // Returns the contained value. // REQUIRES: `ok()` is true. auto operator->() const -> const T* { - CHECK(ok()); + CARBON_CHECK(ok()); return &std::get(val_); } @@ -135,28 +135,29 @@ class ErrorBuilder { } // namespace Carbon // Macro hackery to get a unique variable name. -#define MAKE_UNIQUE_NAME_IMPL(a, b, c) a##b##c -#define MAKE_UNIQUE_NAME(a, b, c) MAKE_UNIQUE_NAME_IMPL(a, b, c) +#define CARBON_MAKE_UNIQUE_NAME_IMPL(a, b, c) a##b##c +#define CARBON_MAKE_UNIQUE_NAME(a, b, c) CARBON_MAKE_UNIQUE_NAME_IMPL(a, b, c) -#define RETURN_IF_ERROR_IMPL(unique_name, expr) \ +#define CARBON_RETURN_IF_ERROR_IMPL(unique_name, expr) \ if (auto unique_name = (expr); /* NOLINT(bugprone-macro-parentheses) */ \ !(unique_name).ok()) { \ return std::move(unique_name).error(); \ } -#define RETURN_IF_ERROR(expr) \ - RETURN_IF_ERROR_IMPL( \ - MAKE_UNIQUE_NAME(_llvm_error_line, __LINE__, __COUNTER__), expr) +#define CARBON_RETURN_IF_ERROR(expr) \ + CARBON_RETURN_IF_ERROR_IMPL( \ + CARBON_MAKE_UNIQUE_NAME(_llvm_error_line, __LINE__, __COUNTER__), expr) -#define ASSIGN_OR_RETURN_IMPL(unique_name, var, expr) \ +#define CARBON_ASSIGN_OR_RETURN_IMPL(unique_name, var, expr) \ auto unique_name = (expr); /* NOLINT(bugprone-macro-parentheses) */ \ if (!(unique_name).ok()) { \ return std::move(unique_name).error(); \ } \ var = std::move(*(unique_name)); /* NOLINT(bugprone-macro-parentheses) */ -#define ASSIGN_OR_RETURN(var, expr) \ - ASSIGN_OR_RETURN_IMPL( \ - MAKE_UNIQUE_NAME(_llvm_expected_line, __LINE__, __COUNTER__), var, expr) +#define CARBON_ASSIGN_OR_RETURN(var, expr) \ + CARBON_ASSIGN_OR_RETURN_IMPL( \ + CARBON_MAKE_UNIQUE_NAME(_llvm_expected_line, __LINE__, __COUNTER__), \ + var, expr) #endif // COMMON_ERROR_H_ diff --git a/common/error_test.cpp b/common/error_test.cpp index c1b976dae442..0d9e836fd26f 100644 --- a/common/error_test.cpp +++ b/common/error_test.cpp @@ -51,8 +51,8 @@ TEST(ErrorTest, IndirectErrorOrSuccess) { TEST(ErrorTest, ReturnIfErrorNoError) { auto result = []() -> ErrorOr { - RETURN_IF_ERROR(ErrorOr(Success())); - RETURN_IF_ERROR(ErrorOr(Success())); + CARBON_RETURN_IF_ERROR(ErrorOr(Success())); + CARBON_RETURN_IF_ERROR(ErrorOr(Success())); return Success(); }(); EXPECT_TRUE(result.ok()); @@ -60,8 +60,8 @@ TEST(ErrorTest, ReturnIfErrorNoError) { TEST(ErrorTest, ReturnIfErrorHasError) { auto result = []() -> ErrorOr { - RETURN_IF_ERROR(ErrorOr(Success())); - RETURN_IF_ERROR(ErrorOr(Error("error"))); + CARBON_RETURN_IF_ERROR(ErrorOr(Success())); + CARBON_RETURN_IF_ERROR(ErrorOr(Error("error"))); return Success(); }(); ASSERT_FALSE(result.ok()); @@ -70,10 +70,10 @@ TEST(ErrorTest, ReturnIfErrorHasError) { TEST(ErrorTest, AssignOrReturnNoError) { auto result = []() -> ErrorOr { - ASSIGN_OR_RETURN(int a, ErrorOr(1)); - ASSIGN_OR_RETURN(const int b, ErrorOr(2)); + CARBON_ASSIGN_OR_RETURN(int a, ErrorOr(1)); + CARBON_ASSIGN_OR_RETURN(const int b, ErrorOr(2)); int c = 0; - ASSIGN_OR_RETURN(c, ErrorOr(3)); + CARBON_ASSIGN_OR_RETURN(c, ErrorOr(3)); return a + b + c; }(); ASSERT_TRUE(result.ok()); @@ -82,7 +82,7 @@ TEST(ErrorTest, AssignOrReturnNoError) { TEST(ErrorTest, AssignOrReturnHasDirectError) { auto result = []() -> ErrorOr { - RETURN_IF_ERROR(ErrorOr(Error("error"))); + CARBON_RETURN_IF_ERROR(ErrorOr(Error("error"))); return 0; }(); ASSERT_FALSE(result.ok()); @@ -90,7 +90,7 @@ TEST(ErrorTest, AssignOrReturnHasDirectError) { TEST(ErrorTest, AssignOrReturnHasErrorInExpected) { auto result = []() -> ErrorOr { - ASSIGN_OR_RETURN(int a, ErrorOr(Error("error"))); + CARBON_ASSIGN_OR_RETURN(int a, ErrorOr(Error("error"))); return a; }(); ASSERT_FALSE(result.ok()); diff --git a/common/string_helpers.cpp b/common/string_helpers.cpp index f3ceb1eb5461..cb010dcee2b9 100644 --- a/common/string_helpers.cpp +++ b/common/string_helpers.cpp @@ -80,7 +80,7 @@ auto UnescapeStringLiteral(llvm::StringRef source, bool is_block_string) break; } case 'u': - FATAL() << "\\u is not yet supported in string literals"; + CARBON_FATAL() << "\\u is not yet supported in string literals"; case '\n': if (!is_block_string) { return std::nullopt; diff --git a/explorer/ast/declaration.cpp b/explorer/ast/declaration.cpp index 1f87d32afb0a..2402c525833d 100644 --- a/explorer/ast/declaration.cpp +++ b/explorer/ast/declaration.cpp @@ -163,7 +163,7 @@ void ReturnTerm::Print(llvm::raw_ostream& out) const { out << "-> auto"; return; case ReturnKind::Expression: - CHECK(type_expression_.has_value()); + CARBON_CHECK(type_expression_.has_value()); out << "-> " << **type_expression_; return; } diff --git a/explorer/ast/declaration.h b/explorer/ast/declaration.h index af33fb172b19..97d5322aa5e9 100644 --- a/explorer/ast/declaration.h +++ b/explorer/ast/declaration.h @@ -59,7 +59,7 @@ class Declaration : public AstNode { // Sets the static type of the declared entity. Can only be called once, // during typechecking. void set_static_type(Nonnull type) { - CHECK(!static_type_.has_value()); + CARBON_CHECK(!static_type_.has_value()); static_type_ = type; } @@ -71,7 +71,7 @@ class Declaration : public AstNode { // Sets the value returned by constant_value(). Can only be called once, // during typechecking. void set_constant_value(Nonnull value) { - CHECK(!constant_value_.has_value()); + CARBON_CHECK(!constant_value_.has_value()); constant_value_ = value; } diff --git a/explorer/ast/expression.cpp b/explorer/ast/expression.cpp index 731ef664246b..3eb3c43eb814 100644 --- a/explorer/ast/expression.cpp +++ b/explorer/ast/expression.cpp @@ -130,7 +130,8 @@ void Expression::Print(llvm::raw_ostream& out) const { << *op.arguments()[1]; break; default: - FATAL() << "Unexpected argument count: " << op.arguments().size(); + CARBON_FATAL() << "Unexpected argument count: " + << op.arguments().size(); } out << ")"; break; diff --git a/explorer/ast/expression.h b/explorer/ast/expression.h index 7f9305e3d466..1c1f49681073 100644 --- a/explorer/ast/expression.h +++ b/explorer/ast/expression.h @@ -46,14 +46,14 @@ class Expression : public AstNode { // The static type of this expression. Cannot be called before typechecking. auto static_type() const -> const Value& { - CHECK(static_type_.has_value()); + CARBON_CHECK(static_type_.has_value()); return **static_type_; } // Sets the static type of this expression. Can only be called once, during // typechecking. void set_static_type(Nonnull type) { - CHECK(!static_type_.has_value()); + CARBON_CHECK(!static_type_.has_value()); static_type_ = type; } @@ -64,7 +64,8 @@ class Expression : public AstNode { // Sets the value category of this expression. Can be called multiple times, // but the argument must have the same value each time. void set_value_category(ValueCategory value_category) { - CHECK(!value_category_.has_value() || value_category == *value_category_); + CARBON_CHECK(!value_category_.has_value() || + value_category == *value_category_); value_category_ = value_category; } @@ -135,7 +136,7 @@ class IdentifierExpression : public Expression { // Sets the value returned by value_node. Can be called only once, // during name resolution. void set_value_node(ValueNodeView value_node) { - CHECK(!value_node_.has_value()); + CARBON_CHECK(!value_node_.has_value()); value_node_ = std::move(value_node); } @@ -170,7 +171,7 @@ class FieldAccessExpression : public Expression { // Can only be called once, during typechecking. void set_impl(Nonnull impl) { - CHECK(!impl_.has_value()); + CARBON_CHECK(!impl_.has_value()); impl_ = impl; } @@ -294,7 +295,7 @@ class StructLiteral : public Expression { std::vector fields) : Expression(AstNodeKind::StructLiteral, loc), fields_(std::move(fields)) { - CHECK(!fields_.empty()) + CARBON_CHECK(!fields_.empty()) << "`{}` is represented as a StructTypeLiteral, not a StructLiteral."; } @@ -392,7 +393,7 @@ class CallExpression : public Expression { // Can only be called once, during typechecking. void set_impls(const ImplExpMap& impls) { - CHECK(impls_.empty()); + CARBON_CHECK(impls_.empty()); impls_ = impls; } diff --git a/explorer/ast/impl_binding.h b/explorer/ast/impl_binding.h index 8229c6664028..11bf0b9dec01 100644 --- a/explorer/ast/impl_binding.h +++ b/explorer/ast/impl_binding.h @@ -62,7 +62,7 @@ class ImplBinding : public AstNode { // Sets the static type of the impl. Can only be called once, during // typechecking. void set_static_type(Nonnull type) { - CHECK(!static_type_.has_value()); + CARBON_CHECK(!static_type_.has_value()); static_type_ = type; } auto value_category() const -> ValueCategory { return ValueCategory::Let; } diff --git a/explorer/ast/pattern.h b/explorer/ast/pattern.h index 5670198834f6..99f2a49c44eb 100644 --- a/explorer/ast/pattern.h +++ b/explorer/ast/pattern.h @@ -52,14 +52,14 @@ class Pattern : public AstNode { // The static type of this pattern. Cannot be called before typechecking. auto static_type() const -> const Value& { - CHECK(static_type_.has_value()); + CARBON_CHECK(static_type_.has_value()); return **static_type_; } // Sets the static type of this expression. Can only be called once, during // typechecking. void set_static_type(Nonnull type) { - CHECK(!static_type_.has_value()); + CARBON_CHECK(!static_type_.has_value()); static_type_ = type; } @@ -165,7 +165,7 @@ class BindingPattern : public Pattern { // Sets the value category of the variable being bound. Can only be called // once during typechecking void set_value_category(ValueCategory vc) { - CHECK(!value_category_.has_value()); + CARBON_CHECK(!value_category_.has_value()); value_category_ = vc; } @@ -233,7 +233,7 @@ class GenericBinding : public Pattern { return symbolic_identity_; } void set_symbolic_identity(Nonnull value) { - CHECK(!symbolic_identity_.has_value()); + CARBON_CHECK(!symbolic_identity_.has_value()); symbolic_identity_ = value; } @@ -243,7 +243,7 @@ class GenericBinding : public Pattern { } // Set the impl binding. void set_impl_binding(Nonnull binding) { - CHECK(!impl_binding_.has_value()); + CARBON_CHECK(!impl_binding_.has_value()); impl_binding_ = binding; } @@ -283,8 +283,8 @@ class AlternativePattern : public Pattern { Nonnull alternative, Nonnull arguments) -> ErrorOr> { - ASSIGN_OR_RETURN(Nonnull field_access, - RequireFieldAccess(alternative)); + CARBON_ASSIGN_OR_RETURN(Nonnull field_access, + RequireFieldAccess(alternative)); return arena->New(source_loc, &field_access->aggregate(), field_access->field(), arguments); diff --git a/explorer/ast/return_term.h b/explorer/ast/return_term.h index 71eba999acb4..79b26c918fdb 100644 --- a/explorer/ast/return_term.h +++ b/explorer/ast/return_term.h @@ -66,7 +66,7 @@ class ReturnTerm { // Sets the value of static_type(). Can only be called once, during // typechecking. void set_static_type(Nonnull type) { - CHECK(!static_type_.has_value()); + CARBON_CHECK(!static_type_.has_value()); static_type_ = type; } @@ -80,7 +80,7 @@ class ReturnTerm { explicit ReturnTerm(ReturnKind kind, SourceLocation source_loc) : kind_(kind), source_loc_(source_loc) { - CHECK(kind != ReturnKind::Expression); + CARBON_CHECK(kind != ReturnKind::Expression); } explicit ReturnTerm(Nonnull type_expression) diff --git a/explorer/ast/statement.h b/explorer/ast/statement.h index fa25d8659ef3..07b71539c8e8 100644 --- a/explorer/ast/statement.h +++ b/explorer/ast/statement.h @@ -188,7 +188,7 @@ class Return : public Statement { // Can only be called once, by ResolveControlFlow. void set_function(Nonnull function) { - CHECK(!function_.has_value()); + CARBON_CHECK(!function_.has_value()); function_ = function; } @@ -239,7 +239,7 @@ class Break : public Statement { // Can only be called once, by ResolveControlFlow. void set_loop(Nonnull loop) { - CHECK(!loop_.has_value()); + CARBON_CHECK(!loop_.has_value()); loop_ = loop; } @@ -266,7 +266,7 @@ class Continue : public Statement { // Can only be called once, by ResolveControlFlow. void set_loop(Nonnull loop) { - CHECK(!loop_.has_value()); + CARBON_CHECK(!loop_.has_value()); loop_ = loop; } @@ -343,7 +343,7 @@ class Continuation : public Statement { // Sets the static type of the continuation. Can only be called once, // during typechecking. void set_static_type(Nonnull type) { - CHECK(!static_type_.has_value()); + CARBON_CHECK(!static_type_.has_value()); static_type_ = type; } diff --git a/explorer/ast/static_scope.cpp b/explorer/ast/static_scope.cpp index 002a1daf017a..e217cf7a8e07 100644 --- a/explorer/ast/static_scope.cpp +++ b/explorer/ast/static_scope.cpp @@ -23,8 +23,8 @@ auto StaticScope::Add(std::string name, ValueNodeView entity) auto StaticScope::Resolve(const std::string& name, SourceLocation source_loc) const -> ErrorOr { - ASSIGN_OR_RETURN(std::optional result, - TryResolve(name, source_loc)); + CARBON_ASSIGN_OR_RETURN(std::optional result, + TryResolve(name, source_loc)); if (!result) { return CompilationError(source_loc) << "could not resolve '" << name << "'"; } @@ -40,8 +40,8 @@ auto StaticScope::TryResolve(const std::string& name, } std::optional result; for (Nonnull parent : parent_scopes_) { - ASSIGN_OR_RETURN(std::optional parent_result, - parent->TryResolve(name, source_loc)); + CARBON_ASSIGN_OR_RETURN(std::optional parent_result, + parent->TryResolve(name, source_loc)); if (parent_result.has_value() && result.has_value() && *parent_result != *result) { return CompilationError(source_loc) diff --git a/explorer/fuzzing/ast_to_proto.cpp b/explorer/fuzzing/ast_to_proto.cpp index 42482f3fdc8c..7a6a919cf997 100644 --- a/explorer/fuzzing/ast_to_proto.cpp +++ b/explorer/fuzzing/ast_to_proto.cpp @@ -549,7 +549,7 @@ static auto DeclarationToProto(const Declaration& declaration) } case DeclarationKind::SelfDeclaration: { - FATAL() << "Unreachable SelfDeclaration in DeclarationToProto()."; + CARBON_FATAL() << "Unreachable SelfDeclaration in DeclarationToProto()."; } } return declaration_proto; diff --git a/explorer/fuzzing/fuzzer_util.cpp b/explorer/fuzzing/fuzzer_util.cpp index b697c489595f..65bafd343ddd 100644 --- a/explorer/fuzzing/fuzzer_util.cpp +++ b/explorer/fuzzing/fuzzer_util.cpp @@ -65,7 +65,7 @@ void ParseAndExecute(const Fuzzing::CompilationUnit& compilation_unit) { } const ErrorOr prelude_path = Internal::GetRunfilesFile("carbon/explorer/data/prelude.carbon"); - CHECK(prelude_path.ok()) << prelude_path.error().message(); + CARBON_CHECK(prelude_path.ok()) << prelude_path.error().message(); AddPrelude(*prelude_path, &arena, &ast->declarations); const ErrorOr result = ExecProgram(&arena, *ast, /*trace_stream=*/std::nullopt); diff --git a/explorer/fuzzing/fuzzverter.cpp b/explorer/fuzzing/fuzzverter.cpp index ebf257aa74cb..258652d73105 100644 --- a/explorer/fuzzing/fuzzverter.cpp +++ b/explorer/fuzzing/fuzzverter.cpp @@ -56,7 +56,8 @@ static auto WriteFile(std::string_view s, std::string_view file_name) static auto TextProtoToCarbon(std::string_view input_file_name, std::string_view output_file_name) -> ErrorOr { - ASSIGN_OR_RETURN(const std::string input_contents, ReadFile(input_file_name)); + CARBON_ASSIGN_OR_RETURN(const std::string input_contents, + ReadFile(input_file_name)); Fuzzing::Carbon carbon_proto; if (!google::protobuf::TextFormat::ParseFromString(input_contents, &carbon_proto)) { diff --git a/explorer/interpreter/action.cpp b/explorer/interpreter/action.cpp index 6f8b28bbe336..2ec90d33052e 100644 --- a/explorer/interpreter/action.cpp +++ b/explorer/interpreter/action.cpp @@ -52,18 +52,18 @@ void RuntimeScope::Print(llvm::raw_ostream& out) const { void RuntimeScope::Initialize(ValueNodeView value_node, Nonnull value) { - CHECK(!value_node.constant_value().has_value()); - CHECK(value->kind() != Value::Kind::LValue); + CARBON_CHECK(!value_node.constant_value().has_value()); + CARBON_CHECK(value->kind() != Value::Kind::LValue); allocations_.push_back(heap_->AllocateValue(value)); auto [it, success] = locals_.insert( {value_node, heap_->arena().New(Address(allocations_.back()))}); - CHECK(success) << "Duplicate definition of " << value_node.base(); + CARBON_CHECK(success) << "Duplicate definition of " << value_node.base(); } void RuntimeScope::Merge(RuntimeScope other) { - CHECK(heap_ == other.heap_); + CARBON_CHECK(heap_ == other.heap_); locals_.merge(other.locals_); - CHECK(other.locals_.empty()) + CARBON_CHECK(other.locals_.empty()) << "Duplicate definition of " << other.locals_.size() << " names, including " << other.locals_.begin()->first.base(); allocations_.insert(allocations_.end(), other.allocations_.begin(), @@ -83,10 +83,10 @@ auto RuntimeScope::Get(ValueNodeView value_node) const auto RuntimeScope::Capture( const std::vector>& scopes) -> RuntimeScope { - CHECK(!scopes.empty()); + CARBON_CHECK(!scopes.empty()); RuntimeScope result(scopes.front()->heap_); for (Nonnull scope : scopes) { - CHECK(scope->heap_ == result.heap_); + CARBON_CHECK(scope->heap_ == result.heap_); for (const auto& entry : scope->locals_) { // Intentionally disregards duplicates later in the vector. result.locals_.insert(entry); diff --git a/explorer/interpreter/action.h b/explorer/interpreter/action.h index b213108b10c3..720b3370e0a3 100644 --- a/explorer/interpreter/action.h +++ b/explorer/interpreter/action.h @@ -97,7 +97,7 @@ class Action { // Resets this Action to its initial state. void Clear() { - CHECK(!scope_.has_value()); + CARBON_CHECK(!scope_.has_value()); pos_ = 0; results_.clear(); } @@ -127,7 +127,7 @@ class Action { // Action is completed or unwound. Can only be called once on a given // Action. void StartScope(RuntimeScope scope) { - CHECK(!scope_.has_value()); + CARBON_CHECK(!scope_.has_value()); scope_ = std::move(scope); } diff --git a/explorer/interpreter/action_stack.cpp b/explorer/interpreter/action_stack.cpp index 7df3fa829893..731ff9ba02c4 100644 --- a/explorer/interpreter/action_stack.cpp +++ b/explorer/interpreter/action_stack.cpp @@ -33,7 +33,7 @@ void ActionStack::PrintScopes(llvm::raw_ostream& out) const { void ActionStack::Start(std::unique_ptr action) { result_ = std::nullopt; - CHECK(todo_.IsEmpty()); + CARBON_CHECK(todo_.IsEmpty()); todo_.Push(std::move(action)); } @@ -92,7 +92,7 @@ void ActionStack::MergeScope(RuntimeScope scope) { globals_->Merge(std::move(scope)); return; } - FATAL() << "No current scope"; + CARBON_FATAL() << "No current scope"; } void ActionStack::InitializeFragment(ContinuationValue::StackFragment& fragment, @@ -118,9 +118,9 @@ auto ActionStack::FinishAction() -> ErrorOr { case Action::Kind::ExpressionAction: case Action::Kind::LValAction: case Action::Kind::PatternAction: - FATAL() << "This kind of action must produce a result: " << *act; + CARBON_FATAL() << "This kind of action must produce a result: " << *act; case Action::Kind::ScopeAction: - FATAL() << "ScopeAction at top of stack"; + CARBON_FATAL() << "ScopeAction at top of stack"; case Action::Kind::StatementAction: case Action::Kind::DeclarationAction: PopScopes(); @@ -134,9 +134,9 @@ auto ActionStack::FinishAction(Nonnull result) switch (act->kind()) { case Action::Kind::StatementAction: case Action::Kind::DeclarationAction: - FATAL() << "This kind of Action cannot produce results: " << *act; + CARBON_FATAL() << "This kind of Action cannot produce results: " << *act; case Action::Kind::ScopeAction: - FATAL() << "ScopeAction at top of stack"; + CARBON_FATAL() << "ScopeAction at top of stack"; case Action::Kind::ExpressionAction: case Action::Kind::LValAction: case Action::Kind::PatternAction: @@ -184,7 +184,7 @@ auto ActionStack::UnwindTo(Nonnull ast_node) auto ActionStack::UnwindPast(Nonnull ast_node) -> ErrorOr { - RETURN_IF_ERROR(UnwindTo(ast_node)); + CARBON_RETURN_IF_ERROR(UnwindTo(ast_node)); todo_.Pop(); PopScopes(); return Success(); @@ -192,7 +192,7 @@ auto ActionStack::UnwindPast(Nonnull ast_node) auto ActionStack::UnwindPast(Nonnull ast_node, Nonnull result) -> ErrorOr { - RETURN_IF_ERROR(UnwindPast(ast_node)); + CARBON_RETURN_IF_ERROR(UnwindPast(ast_node)); SetResult(result); return Success(); } diff --git a/explorer/interpreter/exec_program.cpp b/explorer/interpreter/exec_program.cpp index 5ef657774669..9e40cca64fae 100644 --- a/explorer/interpreter/exec_program.cpp +++ b/explorer/interpreter/exec_program.cpp @@ -35,15 +35,15 @@ auto ExecProgram(Nonnull arena, AST ast, if (trace_stream) { **trace_stream << "********** resolving names **********\n"; } - RETURN_IF_ERROR(ResolveNames(ast)); + CARBON_RETURN_IF_ERROR(ResolveNames(ast)); if (trace_stream) { **trace_stream << "********** resolving control flow **********\n"; } - RETURN_IF_ERROR(ResolveControlFlow(ast)); + CARBON_RETURN_IF_ERROR(ResolveControlFlow(ast)); if (trace_stream) { **trace_stream << "********** type checking **********\n"; } - RETURN_IF_ERROR(TypeChecker(arena, trace_stream).TypeCheck(ast)); + CARBON_RETURN_IF_ERROR(TypeChecker(arena, trace_stream).TypeCheck(ast)); if (trace_stream) { **trace_stream << "\n"; **trace_stream << "********** type checking complete **********\n"; diff --git a/explorer/interpreter/heap.cpp b/explorer/interpreter/heap.cpp index 00f1adee3a77..c2f8ffd19ad0 100644 --- a/explorer/interpreter/heap.cpp +++ b/explorer/interpreter/heap.cpp @@ -23,17 +23,17 @@ auto Heap::AllocateValue(Nonnull v) -> AllocationId { auto Heap::Read(const Address& a, SourceLocation source_loc) const -> ErrorOr> { - RETURN_IF_ERROR(this->CheckAlive(a.allocation_, source_loc)); + CARBON_RETURN_IF_ERROR(this->CheckAlive(a.allocation_, source_loc)); return values_[a.allocation_.index_]->GetField(arena_, a.field_path_, source_loc); } auto Heap::Write(const Address& a, Nonnull v, SourceLocation source_loc) -> ErrorOr { - RETURN_IF_ERROR(this->CheckAlive(a.allocation_, source_loc)); - ASSIGN_OR_RETURN(values_[a.allocation_.index_], - values_[a.allocation_.index_]->SetField( - arena_, a.field_path_, v, source_loc)); + CARBON_RETURN_IF_ERROR(this->CheckAlive(a.allocation_, source_loc)); + CARBON_ASSIGN_OR_RETURN(values_[a.allocation_.index_], + values_[a.allocation_.index_]->SetField( + arena_, a.field_path_, v, source_loc)); return Success(); } @@ -51,8 +51,8 @@ void Heap::Deallocate(AllocationId allocation) { if (alive_[allocation.index_]) { alive_[allocation.index_] = false; } else { - FATAL() << "deallocating an already dead value: " - << *values_[allocation.index_]; + CARBON_FATAL() << "deallocating an already dead value: " + << *values_[allocation.index_]; } } diff --git a/explorer/interpreter/impl_scope.cpp b/explorer/interpreter/impl_scope.cpp index 39136b75b7ab..427e50c537b6 100644 --- a/explorer/interpreter/impl_scope.cpp +++ b/explorer/interpreter/impl_scope.cpp @@ -38,7 +38,7 @@ auto ImplScope::Resolve(Nonnull iface_type, Nonnull type, SourceLocation source_loc, const TypeChecker& type_checker) const -> ErrorOr> { - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( std::optional> result, TryResolve(iface_type, type, source_loc, *this, type_checker)); if (!result.has_value()) { @@ -54,13 +54,13 @@ auto ImplScope::TryResolve(Nonnull iface_type, const ImplScope& original_scope, const TypeChecker& type_checker) const -> ErrorOr>> { - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( std::optional> result, ResolveHere(iface_type, type, source_loc, original_scope, type_checker)); for (Nonnull parent : parent_scopes_) { - ASSIGN_OR_RETURN(std::optional> parent_result, - parent->TryResolve(iface_type, type, source_loc, - original_scope, type_checker)); + CARBON_ASSIGN_OR_RETURN(std::optional> parent_result, + parent->TryResolve(iface_type, type, source_loc, + original_scope, type_checker)); if (parent_result.has_value()) { if (result.has_value()) { return CompilationError(source_loc) << "ambiguous implementations of " @@ -80,7 +80,7 @@ auto ImplScope::ResolveHere(Nonnull iface_type, const TypeChecker& type_checker) const -> ErrorOr>> { if (iface_type->kind() != Value::Kind::InterfaceType) { - FATAL() << "expected an interface, not " << *iface_type; + CARBON_FATAL() << "expected an interface, not " << *iface_type; } const auto& iface = cast(*iface_type); std::optional> result = std::nullopt; diff --git a/explorer/interpreter/interpreter.cpp b/explorer/interpreter/interpreter.cpp index 938d34865a1e..1376694de771 100644 --- a/explorer/interpreter/interpreter.cpp +++ b/explorer/interpreter/interpreter.cpp @@ -196,7 +196,7 @@ auto Interpreter::EvalPrim(Operator op, auto Interpreter::CreateStruct(const std::vector& fields, const std::vector>& values) -> Nonnull { - CHECK(fields.size() == values.size()); + CARBON_CHECK(fields.size() == values.size()); std::vector elements; for (size_t i = 0; i < fields.size(); ++i) { elements.push_back({.name = fields[i].name(), .value = values[i]}); @@ -216,7 +216,7 @@ auto PatternMatch(Nonnull p, Nonnull v, } switch (p->kind()) { case Value::Kind::BindingPlaceholderValue: { - CHECK(bindings.has_value()); + CARBON_CHECK(bindings.has_value()); const auto& placeholder = cast(*p); if (placeholder.value_node().has_value()) { (*bindings)->Initialize(*placeholder.value_node(), v); @@ -233,7 +233,7 @@ auto PatternMatch(Nonnull p, Nonnull v, case Value::Kind::TupleValue: { const auto& p_tup = cast(*p); const auto& v_tup = cast(*v); - CHECK(p_tup.elements().size() == v_tup.elements().size()); + CARBON_CHECK(p_tup.elements().size() == v_tup.elements().size()); for (size_t i = 0; i < p_tup.elements().size(); ++i) { if (!PatternMatch(p_tup.elements()[i], v_tup.elements()[i], source_loc, bindings, generic_args, @@ -244,14 +244,15 @@ auto PatternMatch(Nonnull p, Nonnull v, return true; } default: - FATAL() << "expected a tuple value in pattern, not " << *v; + CARBON_FATAL() << "expected a tuple value in pattern, not " << *v; } case Value::Kind::StructValue: { const auto& p_struct = cast(*p); const auto& v_struct = cast(*v); - CHECK(p_struct.elements().size() == v_struct.elements().size()); + CARBON_CHECK(p_struct.elements().size() == v_struct.elements().size()); for (size_t i = 0; i < p_struct.elements().size(); ++i) { - CHECK(p_struct.elements()[i].name == v_struct.elements()[i].name); + CARBON_CHECK(p_struct.elements()[i].name == + v_struct.elements()[i].name); if (!PatternMatch(p_struct.elements()[i].value, v_struct.elements()[i].value, source_loc, bindings, generic_args, trace_stream)) { @@ -273,7 +274,8 @@ auto PatternMatch(Nonnull p, Nonnull v, bindings, generic_args, trace_stream); } default: - FATAL() << "expected a choice alternative in pattern, not " << *v; + CARBON_FATAL() << "expected a choice alternative in pattern, not " + << *v; } case Value::Kind::FunctionType: switch (v->kind()) { @@ -313,11 +315,11 @@ auto Interpreter::StepLvalue() -> ErrorOr { case ExpressionKind::IdentifierExpression: { // { {x :: C, E, F} :: S, H} // -> { {E(x) :: C, E, F} :: S, H} - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( Nonnull value, todo_.ValueOfNode(cast(exp).value_node(), exp.source_loc())); - CHECK(isa(value)) << *value; + CARBON_CHECK(isa(value)) << *value; return todo_.FinishAction(value); } case ExpressionKind::FieldAccessExpression: { @@ -358,8 +360,8 @@ auto Interpreter::StepLvalue() -> ErrorOr { case ExpressionKind::PrimitiveOperatorExpression: { const auto& op = cast(exp); if (op.op() != Operator::Deref) { - FATAL() << "Can't treat primitive operator expression as lvalue: " - << exp; + CARBON_FATAL() + << "Can't treat primitive operator expression as lvalue: " << exp; } if (act.pos() == 0) { return todo_.Spawn( @@ -387,9 +389,9 @@ auto Interpreter::StepLvalue() -> ErrorOr { case ExpressionKind::IfExpression: case ExpressionKind::ArrayTypeLiteral: case ExpressionKind::InstantiateImpl: - FATAL() << "Can't treat expression as lvalue: " << exp; + CARBON_FATAL() << "Can't treat expression as lvalue: " << exp; case ExpressionKind::UnimplementedExpression: - FATAL() << "Unimplemented: " << exp; + CARBON_FATAL() << "Unimplemented: " << exp; } } @@ -398,28 +400,28 @@ auto Interpreter::EvalImplExp(Nonnull exp) const switch (exp->kind()) { case ExpressionKind::InstantiateImpl: { const InstantiateImpl& inst_impl = cast(*exp); - ASSIGN_OR_RETURN(Nonnull gen_impl, - EvalImplExp(inst_impl.generic_impl())); + CARBON_ASSIGN_OR_RETURN(Nonnull gen_impl, + EvalImplExp(inst_impl.generic_impl())); ImplWitnessMap witnesses; for (auto& [bind, impl_exp] : inst_impl.impls()) { - ASSIGN_OR_RETURN(witnesses[bind], EvalImplExp(impl_exp)); + CARBON_ASSIGN_OR_RETURN(witnesses[bind], EvalImplExp(impl_exp)); } return arena_->New(&gen_impl->declaration(), inst_impl.type_args(), witnesses); } case ExpressionKind::IdentifierExpression: { const auto& ident = cast(*exp); - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( Nonnull value, todo_.ValueOfNode(ident.value_node(), ident.source_loc())); if (const auto* lvalue = dyn_cast(value)) { - ASSIGN_OR_RETURN(value, - heap_.Read(lvalue->address(), exp->source_loc())); + CARBON_ASSIGN_OR_RETURN( + value, heap_.Read(lvalue->address(), exp->source_loc())); } return cast(value); } default: { - FATAL() << "EvalImplExp, unexpected expression: " << *exp; + CARBON_FATAL() << "EvalImplExp, unexpected expression: " << *exp; } } } @@ -429,11 +431,12 @@ auto Interpreter::InstantiateType(Nonnull type, -> ErrorOr> { switch (type->kind()) { case Value::Kind::VariableType: { - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( Nonnull value, todo_.ValueOfNode(&cast(*type).binding(), source_loc)); if (const auto* lvalue = dyn_cast(value)) { - ASSIGN_OR_RETURN(value, heap_.Read(lvalue->address(), source_loc)); + CARBON_ASSIGN_OR_RETURN(value, + heap_.Read(lvalue->address(), source_loc)); } return value; } @@ -441,12 +444,12 @@ auto Interpreter::InstantiateType(Nonnull type, const auto& class_type = cast(*type); BindingMap inst_type_args; for (const auto& [ty_var, ty_arg] : class_type.type_args()) { - ASSIGN_OR_RETURN(inst_type_args[ty_var], - InstantiateType(ty_arg, source_loc)); + CARBON_ASSIGN_OR_RETURN(inst_type_args[ty_var], + InstantiateType(ty_arg, source_loc)); } std::map, Nonnull> witnesses; for (const auto& [bind, impl_exp] : class_type.impls()) { - ASSIGN_OR_RETURN(witnesses[bind], EvalImplExp(impl_exp)); + CARBON_ASSIGN_OR_RETURN(witnesses[bind], EvalImplExp(impl_exp)); } return arena_->New(&class_type.declaration(), inst_type_args, witnesses); @@ -493,8 +496,8 @@ auto Interpreter::Convert(Nonnull value, case Value::Kind::TypeOfChoiceType: case Value::Kind::TypeOfParameterizedEntityName: case Value::Kind::StaticArrayType: - // TODO: add `CHECK(TypeEqual(type, value->dynamic_type()))`, once we - // have Value::dynamic_type. + // TODO: add `CARBON_CHECK(TypeEqual(type, value->dynamic_type()))`, once + // we have Value::dynamic_type. return value; case Value::Kind::StructValue: { const auto& struct_val = cast(*value); @@ -507,8 +510,9 @@ auto Interpreter::Convert(Nonnull value, destination_struct_type.fields()) { std::optional> old_value = struct_val.FindField(field_name); - ASSIGN_OR_RETURN(Nonnull val, - Convert(*old_value, field_type, source_loc)); + CARBON_ASSIGN_OR_RETURN( + Nonnull val, + Convert(*old_value, field_type, source_loc)); new_elements.push_back({.name = field_name, .value = val}); } return arena_->New(std::move(new_elements)); @@ -516,13 +520,14 @@ auto Interpreter::Convert(Nonnull value, case Value::Kind::NominalClassType: { // Instantiate the `destintation_type` to obtain the runtime // type of the object. - ASSIGN_OR_RETURN(Nonnull inst_dest, - InstantiateType(destination_type, source_loc)); + CARBON_ASSIGN_OR_RETURN( + Nonnull inst_dest, + InstantiateType(destination_type, source_loc)); return arena_->New(inst_dest, value); } default: - FATAL() << "Can't convert value " << *value << " to type " - << *destination_type; + CARBON_FATAL() << "Can't convert value " << *value << " to type " + << *destination_type; } } case Value::Kind::TupleValue: { @@ -540,15 +545,17 @@ auto Interpreter::Convert(Nonnull value, break; } default: - FATAL() << "Can't convert value " << *value << " to type " - << *destination_type; + CARBON_FATAL() << "Can't convert value " << *value << " to type " + << *destination_type; } - CHECK(tuple->elements().size() == destination_element_types.size()); + CARBON_CHECK(tuple->elements().size() == + destination_element_types.size()); std::vector> new_elements; for (size_t i = 0; i < tuple->elements().size(); ++i) { - ASSIGN_OR_RETURN(Nonnull val, - Convert(tuple->elements()[i], - destination_element_types[i], source_loc)); + CARBON_ASSIGN_OR_RETURN( + Nonnull val, + Convert(tuple->elements()[i], destination_element_types[i], + source_loc)); new_elements.push_back(val); } return arena_->New(std::move(new_elements)); @@ -573,9 +580,10 @@ auto Interpreter::CallFunction(const CallExpression& call, case Value::Kind::FunctionValue: { const FunctionValue& fun_val = cast(*fun); const FunctionDeclaration& function = fun_val.declaration(); - ASSIGN_OR_RETURN(Nonnull converted_args, - Convert(arg, &function.param_pattern().static_type(), - call.source_loc())); + CARBON_ASSIGN_OR_RETURN( + Nonnull converted_args, + Convert(arg, &function.param_pattern().static_type(), + call.source_loc())); RuntimeScope function_scope(&heap_); // Bring the class type arguments into scope. for (const auto& [bind, val] : fun_val.type_args()) { @@ -593,10 +601,10 @@ auto Interpreter::CallFunction(const CallExpression& call, function_scope.Initialize(impl_bind, witness); } BindingMap generic_args; - CHECK(PatternMatch(&function.param_pattern().value(), converted_args, - call.source_loc(), &function_scope, generic_args, - trace_stream_)); - CHECK(function.body().has_value()) + CARBON_CHECK(PatternMatch(&function.param_pattern().value(), + converted_args, call.source_loc(), + &function_scope, generic_args, trace_stream_)); + CARBON_CHECK(function.body().has_value()) << "Calling a function that's missing a body"; return todo_.Spawn(std::make_unique(*function.body()), std::move(function_scope)); @@ -604,18 +612,19 @@ auto Interpreter::CallFunction(const CallExpression& call, case Value::Kind::BoundMethodValue: { const auto& m = cast(*fun); const FunctionDeclaration& method = m.declaration(); - CHECK(method.is_method()); - ASSIGN_OR_RETURN(Nonnull converted_args, - Convert(arg, &method.param_pattern().static_type(), - call.source_loc())); + CARBON_CHECK(method.is_method()); + CARBON_ASSIGN_OR_RETURN( + Nonnull converted_args, + Convert(arg, &method.param_pattern().static_type(), + call.source_loc())); RuntimeScope method_scope(&heap_); BindingMap generic_args; - CHECK(PatternMatch(&method.me_pattern().value(), m.receiver(), - call.source_loc(), &method_scope, generic_args, - trace_stream_)); - CHECK(PatternMatch(&method.param_pattern().value(), converted_args, - call.source_loc(), &method_scope, generic_args, - trace_stream_)); + CARBON_CHECK(PatternMatch(&method.me_pattern().value(), m.receiver(), + call.source_loc(), &method_scope, generic_args, + trace_stream_)); + CARBON_CHECK(PatternMatch(&method.param_pattern().value(), converted_args, + call.source_loc(), &method_scope, generic_args, + trace_stream_)); // Bring the class type arguments into scope. for (const auto& [bind, val] : m.type_args()) { method_scope.Initialize(bind, val); @@ -625,7 +634,7 @@ auto Interpreter::CallFunction(const CallExpression& call, for (const auto& [impl_bind, witness] : m.witnesses()) { method_scope.Initialize(impl_bind, witness); } - CHECK(method.body().has_value()) + CARBON_CHECK(method.body().has_value()) << "Calling a method that's missing a body"; return todo_.Spawn(std::make_unique(*method.body()), std::move(method_scope)); @@ -635,8 +644,8 @@ auto Interpreter::CallFunction(const CallExpression& call, const Declaration& decl = name.declaration(); RuntimeScope params_scope(&heap_); BindingMap generic_args; - CHECK(PatternMatch(&name.params().value(), arg, call.source_loc(), - ¶ms_scope, generic_args, trace_stream_)); + CARBON_CHECK(PatternMatch(&name.params().value(), arg, call.source_loc(), + ¶ms_scope, generic_args, trace_stream_)); switch (decl.kind()) { case DeclarationKind::ClassDeclaration: { switch (phase()) { @@ -660,7 +669,7 @@ auto Interpreter::CallFunction(const CallExpression& call, } } default: - FATAL() << "unknown kind of ParameterizedEntityName " << decl; + CARBON_FATAL() << "unknown kind of ParameterizedEntityName " << decl; } } default: @@ -768,42 +777,43 @@ auto Interpreter::StepExp() -> ErrorOr { // -> { { v_f :: C, E, F} : S, H} std::optional> witness = std::nullopt; if (access.impl().has_value()) { - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( auto witness_addr, todo_.ValueOfNode(*access.impl(), access.source_loc())); - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( Nonnull witness_value, heap_.Read(llvm::cast(witness_addr)->address(), access.source_loc())); witness = cast(witness_value); } FieldPath::Component field(access.field(), witness); - ASSIGN_OR_RETURN(Nonnull member, - act.results()[0]->GetField(arena_, FieldPath(field), - exp.source_loc())); + CARBON_ASSIGN_OR_RETURN( + Nonnull member, + act.results()[0]->GetField(arena_, FieldPath(field), + exp.source_loc())); return todo_.FinishAction(member); } } case ExpressionKind::IdentifierExpression: { - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); const auto& ident = cast(exp); // { {x :: C, E, F} :: S, H} -> { {H(E(x)) :: C, E, F} :: S, H} - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( Nonnull value, todo_.ValueOfNode(ident.value_node(), ident.source_loc())); if (const auto* lvalue = dyn_cast(value)) { - ASSIGN_OR_RETURN(value, - heap_.Read(lvalue->address(), exp.source_loc())); + CARBON_ASSIGN_OR_RETURN( + value, heap_.Read(lvalue->address(), exp.source_loc())); } return todo_.FinishAction(value); } case ExpressionKind::IntLiteral: - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); // { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H} return todo_.FinishAction( arena_->New(cast(exp).value())); case ExpressionKind::BoolLiteral: - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); // { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H} return todo_.FinishAction( arena_->New(cast(exp).value())); @@ -821,8 +831,9 @@ auto Interpreter::StepExp() -> ErrorOr { } else { // { {v :: op(vs,[]) :: C, E, F} :: S, H} // -> { {eval_prim(op, (vs,v)) :: C, E, F} :: S, H} - ASSIGN_OR_RETURN(Nonnull value, - EvalPrim(op.op(), act.results(), exp.source_loc())); + CARBON_ASSIGN_OR_RETURN( + Nonnull value, + EvalPrim(op.op(), act.results(), exp.source_loc())); return todo_.FinishAction(value); } } @@ -866,7 +877,7 @@ auto Interpreter::StepExp() -> ErrorOr { return todo_.FinishAction(act.results()[2 + int(num_impls)]); } } else { - FATAL() << "in StepExp with Call pos " << act.pos(); + CARBON_FATAL() << "in StepExp with Call pos " << act.pos(); } } case ExpressionKind::IntrinsicExpression: { @@ -886,15 +897,15 @@ auto Interpreter::StepExp() -> ErrorOr { } } case ExpressionKind::IntTypeLiteral: { - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); return todo_.FinishAction(arena_->New()); } case ExpressionKind::BoolTypeLiteral: { - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); return todo_.FinishAction(arena_->New()); } case ExpressionKind::TypeTypeLiteral: { - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); return todo_.FinishAction(arena_->New()); } case ExpressionKind::FunctionTypeLiteral: { @@ -915,16 +926,16 @@ auto Interpreter::StepExp() -> ErrorOr { } } case ExpressionKind::ContinuationTypeLiteral: { - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); return todo_.FinishAction(arena_->New()); } case ExpressionKind::StringLiteral: - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); // { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H} return todo_.FinishAction( arena_->New(cast(exp).value())); case ExpressionKind::StringTypeLiteral: { - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); return todo_.FinishAction(arena_->New()); } case ExpressionKind::IfExpression: { @@ -943,7 +954,7 @@ auto Interpreter::StepExp() -> ErrorOr { break; } case ExpressionKind::UnimplementedExpression: - FATAL() << "Unimplemented: " << exp; + CARBON_FATAL() << "Unimplemented: " << exp; case ExpressionKind::ArrayTypeLiteral: { const auto& array_literal = cast(exp); if (act.pos() == 0) { @@ -969,7 +980,7 @@ auto Interpreter::StepPattern() -> ErrorOr { } switch (pattern.kind()) { case PatternKind::AutoPattern: { - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); return todo_.FinishAction(arena_->New()); } case PatternKind::BindingPattern: { @@ -1007,7 +1018,7 @@ auto Interpreter::StepPattern() -> ErrorOr { return todo_.Spawn( std::make_unique(&alternative.arguments())); } else { - CHECK(act.pos() == 2); + CARBON_CHECK(act.pos() == 2); const auto& choice_type = cast(*act.results()[0]); return todo_.FinishAction(arena_->New( alternative.alternative_name(), choice_type.name(), @@ -1056,9 +1067,10 @@ auto Interpreter::StepStmt() -> ErrorOr { auto c = match_stmt.clauses()[clause_num]; RuntimeScope matches(&heap_); BindingMap generic_args; - ASSIGN_OR_RETURN(Nonnull val, - Convert(act.results()[0], &c.pattern().static_type(), - stmt.source_loc())); + CARBON_ASSIGN_OR_RETURN( + Nonnull val, + Convert(act.results()[0], &c.pattern().static_type(), + stmt.source_loc())); if (PatternMatch(&c.pattern().value(), val, stmt.source_loc(), &matches, generic_args, trace_stream_)) { // Ensure we don't process any more clauses. @@ -1078,9 +1090,10 @@ auto Interpreter::StepStmt() -> ErrorOr { return todo_.Spawn( std::make_unique(&cast(stmt).condition())); } else { - ASSIGN_OR_RETURN(Nonnull condition, - Convert(act.results().back(), arena_->New(), - stmt.source_loc())); + CARBON_ASSIGN_OR_RETURN( + Nonnull condition, + Convert(act.results().back(), arena_->New(), + stmt.source_loc())); if (cast(*condition).value()) { // { {true :: (while ([]) s) :: C, E, F} :: S, H} // -> { { s :: (while (e) s) :: C, E, F } :: S, H} @@ -1093,13 +1106,13 @@ auto Interpreter::StepStmt() -> ErrorOr { } } case StatementKind::Break: { - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); // { { break; :: ... :: (while (e) s) :: C, E, F} :: S, H} // -> { { C, E', F} :: S, H} return todo_.UnwindPast(&cast(stmt).loop()); } case StatementKind::Continue: { - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); // { { continue; :: ... :: (while (e) s) :: C, E, F} :: S, H} // -> { { (while (e) s) :: C, E', F} :: S, H} return todo_.UnwindTo(&cast(stmt).loop()); @@ -1130,7 +1143,7 @@ auto Interpreter::StepStmt() -> ErrorOr { } else { // { { v :: (x = []) :: C, E, F} :: S, H} // -> { { C, E(x := a), F} :: S, H(a := copy(v))} - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( Nonnull v, Convert(act.results()[0], &definition.pattern().static_type(), stmt.source_loc())); @@ -1139,8 +1152,8 @@ auto Interpreter::StepStmt() -> ErrorOr { RuntimeScope matches(&heap_); BindingMap generic_args; - CHECK(PatternMatch(p, v, stmt.source_loc(), &matches, generic_args, - trace_stream_)) + CARBON_CHECK(PatternMatch(p, v, stmt.source_loc(), &matches, + generic_args, trace_stream_)) << stmt.source_loc() << ": internal error in variable definition, match failed"; todo_.MergeScope(std::move(matches)); @@ -1170,10 +1183,12 @@ auto Interpreter::StepStmt() -> ErrorOr { // { { v :: (a = []) :: C, E, F} :: S, H} // -> { { C, E, F} :: S, H(a := v)} const auto& lval = cast(*act.results()[0]); - ASSIGN_OR_RETURN(Nonnull rval, - Convert(act.results()[1], &assign.lhs().static_type(), - stmt.source_loc())); - RETURN_IF_ERROR(heap_.Write(lval.address(), rval, stmt.source_loc())); + CARBON_ASSIGN_OR_RETURN( + Nonnull rval, + Convert(act.results()[1], &assign.lhs().static_type(), + stmt.source_loc())); + CARBON_RETURN_IF_ERROR( + heap_.Write(lval.address(), rval, stmt.source_loc())); return todo_.FinishAction(); } } @@ -1184,9 +1199,10 @@ auto Interpreter::StepStmt() -> ErrorOr { return todo_.Spawn( std::make_unique(&cast(stmt).condition())); } else if (act.pos() == 1) { - ASSIGN_OR_RETURN(Nonnull condition, - Convert(act.results()[0], arena_->New(), - stmt.source_loc())); + CARBON_ASSIGN_OR_RETURN( + Nonnull condition, + Convert(act.results()[0], arena_->New(), + stmt.source_loc())); if (cast(*condition).value()) { // { {true :: if ([]) then_stmt else else_stmt :: C, E, F} :: // S, H} @@ -1215,14 +1231,14 @@ auto Interpreter::StepStmt() -> ErrorOr { // { {v :: return [] :: C, E, F} :: {C', E', F'} :: S, H} // -> { {v :: C', E', F'} :: S, H} const FunctionDeclaration& function = cast(stmt).function(); - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( Nonnull return_value, Convert(act.results()[0], &function.return_term().static_type(), stmt.source_loc())); return todo_.UnwindPast(*function.body(), return_value); } case StatementKind::Continuation: { - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); const auto& continuation = cast(stmt); // Create a continuation object by creating a frame similar the // way one is created in a function call. @@ -1247,7 +1263,7 @@ auto Interpreter::StepStmt() -> ErrorOr { } } case StatementKind::Await: - CHECK(act.pos() == 0); + CARBON_CHECK(act.pos() == 0); return todo_.Suspend(); } } @@ -1290,22 +1306,22 @@ auto Interpreter::Step() -> ErrorOr { Action& act = todo_.CurrentAction(); switch (act.kind()) { case Action::Kind::LValAction: - RETURN_IF_ERROR(StepLvalue()); + CARBON_RETURN_IF_ERROR(StepLvalue()); break; case Action::Kind::ExpressionAction: - RETURN_IF_ERROR(StepExp()); + CARBON_RETURN_IF_ERROR(StepExp()); break; case Action::Kind::PatternAction: - RETURN_IF_ERROR(StepPattern()); + CARBON_RETURN_IF_ERROR(StepPattern()); break; case Action::Kind::StatementAction: - RETURN_IF_ERROR(StepStmt()); + CARBON_RETURN_IF_ERROR(StepStmt()); break; case Action::Kind::DeclarationAction: - RETURN_IF_ERROR(StepDeclaration()); + CARBON_RETURN_IF_ERROR(StepDeclaration()); break; case Action::Kind::ScopeAction: - FATAL() << "ScopeAction escaped ActionStack"; + CARBON_FATAL() << "ScopeAction escaped ActionStack"; } // switch return Success(); } @@ -1317,7 +1333,7 @@ auto Interpreter::RunAllSteps(std::unique_ptr action) } todo_.Start(std::move(action)); while (!todo_.IsEmpty()) { - RETURN_IF_ERROR(Step()); + CARBON_RETURN_IF_ERROR(Step()); if (trace_stream_) { PrintState(**trace_stream_); } @@ -1334,7 +1350,7 @@ auto InterpProgram(const AST& ast, Nonnull arena, } for (Nonnull declaration : ast.declarations) { - RETURN_IF_ERROR(interpreter.RunAllSteps( + CARBON_RETURN_IF_ERROR(interpreter.RunAllSteps( std::make_unique(declaration))); } @@ -1342,7 +1358,7 @@ auto InterpProgram(const AST& ast, Nonnull arena, **trace_stream << "********** calling main function **********\n"; } - RETURN_IF_ERROR(interpreter.RunAllSteps( + CARBON_RETURN_IF_ERROR(interpreter.RunAllSteps( std::make_unique(*ast.main_call))); return cast(*interpreter.result()).value(); @@ -1352,7 +1368,7 @@ auto InterpExp(Nonnull e, Nonnull arena, std::optional> trace_stream) -> ErrorOr> { Interpreter interpreter(Phase::CompileTime, arena, trace_stream); - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( interpreter.RunAllSteps(std::make_unique(e))); return interpreter.result(); } @@ -1361,7 +1377,8 @@ auto InterpPattern(Nonnull p, Nonnull arena, std::optional> trace_stream) -> ErrorOr> { Interpreter interpreter(Phase::CompileTime, arena, trace_stream); - RETURN_IF_ERROR(interpreter.RunAllSteps(std::make_unique(p))); + CARBON_RETURN_IF_ERROR( + interpreter.RunAllSteps(std::make_unique(p))); return interpreter.result(); } diff --git a/explorer/interpreter/resolve_control_flow.cpp b/explorer/interpreter/resolve_control_flow.cpp index 0cb11bc43233..235ad58a32eb 100644 --- a/explorer/interpreter/resolve_control_flow.cpp +++ b/explorer/interpreter/resolve_control_flow.cpp @@ -78,10 +78,10 @@ static auto ResolveControlFlow(Nonnull statement, return Success(); case StatementKind::If: { auto& if_stmt = cast(*statement); - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ResolveControlFlow(&if_stmt.then_block(), loop, function)); if (if_stmt.else_block().has_value()) { - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ResolveControlFlow(*if_stmt.else_block(), loop, function)); } return Success(); @@ -89,25 +89,26 @@ static auto ResolveControlFlow(Nonnull statement, case StatementKind::Block: { auto& block = cast(*statement); for (auto* block_statement : block.statements()) { - RETURN_IF_ERROR(ResolveControlFlow(block_statement, loop, function)); + CARBON_RETURN_IF_ERROR( + ResolveControlFlow(block_statement, loop, function)); } return Success(); } case StatementKind::While: - RETURN_IF_ERROR(ResolveControlFlow(&cast(*statement).body(), - statement, function)); + CARBON_RETURN_IF_ERROR(ResolveControlFlow(&cast(*statement).body(), + statement, function)); return Success(); case StatementKind::Match: { auto& match = cast(*statement); for (Match::Clause& clause : match.clauses()) { - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ResolveControlFlow(&clause.statement(), loop, function)); } return Success(); } case StatementKind::Continuation: - RETURN_IF_ERROR(ResolveControlFlow(&cast(*statement).body(), - std::nullopt, std::nullopt)); + CARBON_RETURN_IF_ERROR(ResolveControlFlow( + &cast(*statement).body(), std::nullopt, std::nullopt)); return Success(); case StatementKind::ExpressionStatement: case StatementKind::Assign: @@ -124,7 +125,7 @@ auto ResolveControlFlow(Nonnull declaration) -> ErrorOr { auto& function = cast(*declaration); if (function.body().has_value()) { FunctionData data = {.declaration = &function}; - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ResolveControlFlow(*function.body(), std::nullopt, &data)); } break; @@ -132,21 +133,21 @@ auto ResolveControlFlow(Nonnull declaration) -> ErrorOr { case DeclarationKind::ClassDeclaration: { auto& class_decl = cast(*declaration); for (Nonnull member : class_decl.members()) { - RETURN_IF_ERROR(ResolveControlFlow(member)); + CARBON_RETURN_IF_ERROR(ResolveControlFlow(member)); } break; } case DeclarationKind::InterfaceDeclaration: { auto& iface_decl = cast(*declaration); for (Nonnull member : iface_decl.members()) { - RETURN_IF_ERROR(ResolveControlFlow(member)); + CARBON_RETURN_IF_ERROR(ResolveControlFlow(member)); } break; } case DeclarationKind::ImplDeclaration: { auto& impl_decl = cast(*declaration); for (Nonnull member : impl_decl.members()) { - RETURN_IF_ERROR(ResolveControlFlow(member)); + CARBON_RETURN_IF_ERROR(ResolveControlFlow(member)); } break; } @@ -161,7 +162,7 @@ auto ResolveControlFlow(Nonnull declaration) -> ErrorOr { auto ResolveControlFlow(AST& ast) -> ErrorOr { for (auto declaration : ast.declarations) { - RETURN_IF_ERROR(ResolveControlFlow(declaration)); + CARBON_RETURN_IF_ERROR(ResolveControlFlow(declaration)); } return Success(); } diff --git a/explorer/interpreter/resolve_names.cpp b/explorer/interpreter/resolve_names.cpp index 38a21dfe003c..caadf6d16ecb 100644 --- a/explorer/interpreter/resolve_names.cpp +++ b/explorer/interpreter/resolve_names.cpp @@ -27,7 +27,8 @@ static auto AddExposedNames(const Declaration& declaration, switch (declaration.kind()) { case DeclarationKind::InterfaceDeclaration: { auto& iface_decl = cast(declaration); - RETURN_IF_ERROR(enclosing_scope.Add(iface_decl.name(), &iface_decl)); + CARBON_RETURN_IF_ERROR( + enclosing_scope.Add(iface_decl.name(), &iface_decl)); break; } case DeclarationKind::ImplDeclaration: { @@ -36,30 +37,31 @@ static auto AddExposedNames(const Declaration& declaration, } case DeclarationKind::FunctionDeclaration: { auto& func = cast(declaration); - RETURN_IF_ERROR(enclosing_scope.Add(func.name(), &func)); + CARBON_RETURN_IF_ERROR(enclosing_scope.Add(func.name(), &func)); break; } case DeclarationKind::ClassDeclaration: { auto& class_decl = cast(declaration); - RETURN_IF_ERROR(enclosing_scope.Add(class_decl.name(), &class_decl)); + CARBON_RETURN_IF_ERROR( + enclosing_scope.Add(class_decl.name(), &class_decl)); break; } case DeclarationKind::ChoiceDeclaration: { auto& choice = cast(declaration); - RETURN_IF_ERROR(enclosing_scope.Add(choice.name(), &choice)); + CARBON_RETURN_IF_ERROR(enclosing_scope.Add(choice.name(), &choice)); break; } case DeclarationKind::VariableDeclaration: { auto& var = cast(declaration); if (var.binding().name() != AnonymousName) { - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( enclosing_scope.Add(var.binding().name(), &var.binding())); } break; } case DeclarationKind::SelfDeclaration: { auto& self = cast(declaration); - RETURN_IF_ERROR(enclosing_scope.Add("Self", &self)); + CARBON_RETURN_IF_ERROR(enclosing_scope.Add("Self", &self)); break; } } @@ -92,74 +94,81 @@ static auto ResolveNames(Expression& expression, switch (expression.kind()) { case ExpressionKind::CallExpression: { auto& call = cast(expression); - RETURN_IF_ERROR(ResolveNames(call.function(), enclosing_scope)); - RETURN_IF_ERROR(ResolveNames(call.argument(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(call.function(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(call.argument(), enclosing_scope)); break; } case ExpressionKind::FunctionTypeLiteral: { auto& fun_type = cast(expression); - RETURN_IF_ERROR(ResolveNames(fun_type.parameter(), enclosing_scope)); - RETURN_IF_ERROR(ResolveNames(fun_type.return_type(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(fun_type.parameter(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(fun_type.return_type(), enclosing_scope)); break; } case ExpressionKind::FieldAccessExpression: - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ResolveNames(cast(expression).aggregate(), enclosing_scope)); break; case ExpressionKind::IndexExpression: { auto& index = cast(expression); - RETURN_IF_ERROR(ResolveNames(index.aggregate(), enclosing_scope)); - RETURN_IF_ERROR(ResolveNames(index.offset(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(index.aggregate(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(index.offset(), enclosing_scope)); break; } case ExpressionKind::PrimitiveOperatorExpression: for (Nonnull operand : cast(expression).arguments()) { - RETURN_IF_ERROR(ResolveNames(*operand, enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(*operand, enclosing_scope)); } break; case ExpressionKind::TupleLiteral: for (Nonnull field : cast(expression).fields()) { - RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope)); } break; case ExpressionKind::StructLiteral: for (FieldInitializer& init : cast(expression).fields()) { - RETURN_IF_ERROR(ResolveNames(init.expression(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(init.expression(), enclosing_scope)); } break; case ExpressionKind::StructTypeLiteral: for (FieldInitializer& init : cast(expression).fields()) { - RETURN_IF_ERROR(ResolveNames(init.expression(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(init.expression(), enclosing_scope)); } break; case ExpressionKind::IdentifierExpression: { auto& identifier = cast(expression); - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( const auto value_node, enclosing_scope.Resolve(identifier.name(), identifier.source_loc())); identifier.set_value_node(value_node); break; } case ExpressionKind::IntrinsicExpression: - RETURN_IF_ERROR(ResolveNames(cast(expression).args(), - enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames( + cast(expression).args(), enclosing_scope)); break; case ExpressionKind::IfExpression: { auto& if_expr = cast(expression); - RETURN_IF_ERROR(ResolveNames(if_expr.condition(), enclosing_scope)); - RETURN_IF_ERROR(ResolveNames(if_expr.then_expression(), enclosing_scope)); - RETURN_IF_ERROR(ResolveNames(if_expr.else_expression(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(if_expr.condition(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(if_expr.then_expression(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(if_expr.else_expression(), enclosing_scope)); break; } case ExpressionKind::ArrayTypeLiteral: { auto& array_literal = cast(expression); - RETURN_IF_ERROR(ResolveNames(array_literal.element_type_expression(), - enclosing_scope)); - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR(ResolveNames( + array_literal.element_type_expression(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( ResolveNames(array_literal.size_expression(), enclosing_scope)); break; } @@ -184,39 +193,41 @@ static auto ResolveNames(Pattern& pattern, StaticScope& enclosing_scope) switch (pattern.kind()) { case PatternKind::BindingPattern: { auto& binding = cast(pattern); - RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope)); if (binding.name() != AnonymousName) { - RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding)); + CARBON_RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding)); } break; } case PatternKind::GenericBinding: { auto& binding = cast(pattern); - RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope)); if (binding.name() != AnonymousName) { - RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding)); + CARBON_RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding)); } break; } case PatternKind::TuplePattern: for (Nonnull field : cast(pattern).fields()) { - RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope)); } break; case PatternKind::AlternativePattern: { auto& alternative = cast(pattern); - RETURN_IF_ERROR(ResolveNames(alternative.choice_type(), enclosing_scope)); - RETURN_IF_ERROR(ResolveNames(alternative.arguments(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(alternative.choice_type(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(alternative.arguments(), enclosing_scope)); break; } case PatternKind::ExpressionPattern: - RETURN_IF_ERROR(ResolveNames( + CARBON_RETURN_IF_ERROR(ResolveNames( cast(pattern).expression(), enclosing_scope)); break; case PatternKind::AutoPattern: break; case PatternKind::VarPattern: - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ResolveNames(cast(pattern).pattern(), enclosing_scope)); break; } @@ -227,32 +238,35 @@ static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope) -> ErrorOr { switch (statement.kind()) { case StatementKind::ExpressionStatement: - RETURN_IF_ERROR(ResolveNames( + CARBON_RETURN_IF_ERROR(ResolveNames( cast(statement).expression(), enclosing_scope)); break; case StatementKind::Assign: { auto& assign = cast(statement); - RETURN_IF_ERROR(ResolveNames(assign.lhs(), enclosing_scope)); - RETURN_IF_ERROR(ResolveNames(assign.rhs(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(assign.lhs(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(assign.rhs(), enclosing_scope)); break; } case StatementKind::VariableDefinition: { auto& def = cast(statement); - RETURN_IF_ERROR(ResolveNames(def.init(), enclosing_scope)); - RETURN_IF_ERROR(ResolveNames(def.pattern(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(def.init(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(def.pattern(), enclosing_scope)); break; } case StatementKind::If: { auto& if_stmt = cast(statement); - RETURN_IF_ERROR(ResolveNames(if_stmt.condition(), enclosing_scope)); - RETURN_IF_ERROR(ResolveNames(if_stmt.then_block(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(if_stmt.condition(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(if_stmt.then_block(), enclosing_scope)); if (if_stmt.else_block().has_value()) { - RETURN_IF_ERROR(ResolveNames(**if_stmt.else_block(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(**if_stmt.else_block(), enclosing_scope)); } break; } case StatementKind::Return: - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ResolveNames(cast(statement).expression(), enclosing_scope)); break; case StatementKind::Block: { @@ -260,38 +274,40 @@ static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope) StaticScope block_scope; block_scope.AddParent(&enclosing_scope); for (Nonnull sub_statement : block.statements()) { - RETURN_IF_ERROR(ResolveNames(*sub_statement, block_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(*sub_statement, block_scope)); } break; } case StatementKind::While: { auto& while_stmt = cast(statement); - RETURN_IF_ERROR(ResolveNames(while_stmt.condition(), enclosing_scope)); - RETURN_IF_ERROR(ResolveNames(while_stmt.body(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(while_stmt.condition(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(while_stmt.body(), enclosing_scope)); break; } case StatementKind::Match: { auto& match = cast(statement); - RETURN_IF_ERROR(ResolveNames(match.expression(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(match.expression(), enclosing_scope)); for (Match::Clause& clause : match.clauses()) { StaticScope clause_scope; clause_scope.AddParent(&enclosing_scope); - RETURN_IF_ERROR(ResolveNames(clause.pattern(), clause_scope)); - RETURN_IF_ERROR(ResolveNames(clause.statement(), clause_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(clause.pattern(), clause_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(clause.statement(), clause_scope)); } break; } case StatementKind::Continuation: { auto& continuation = cast(statement); - RETURN_IF_ERROR(enclosing_scope.Add(continuation.name(), &continuation)); + CARBON_RETURN_IF_ERROR( + enclosing_scope.Add(continuation.name(), &continuation)); StaticScope continuation_scope; continuation_scope.AddParent(&enclosing_scope); - RETURN_IF_ERROR(ResolveNames(cast(statement).body(), - continuation_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(cast(statement).body(), + continuation_scope)); break; } case StatementKind::Run: - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ResolveNames(cast(statement).argument(), enclosing_scope)); break; case StatementKind::Await: @@ -310,14 +326,14 @@ static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope) StaticScope iface_scope; iface_scope.AddParent(&enclosing_scope); if (iface.params().has_value()) { - RETURN_IF_ERROR(ResolveNames(**iface.params(), iface_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(**iface.params(), iface_scope)); } - RETURN_IF_ERROR(iface_scope.Add("Self", iface.self())); + CARBON_RETURN_IF_ERROR(iface_scope.Add("Self", iface.self())); for (Nonnull member : iface.members()) { - RETURN_IF_ERROR(AddExposedNames(*member, iface_scope)); + CARBON_RETURN_IF_ERROR(AddExposedNames(*member, iface_scope)); } for (Nonnull member : iface.members()) { - RETURN_IF_ERROR(ResolveNames(*member, iface_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(*member, iface_scope)); } break; } @@ -326,24 +342,24 @@ static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope) StaticScope impl_scope; impl_scope.AddParent(&enclosing_scope); for (Nonnull binding : impl.deduced_parameters()) { - RETURN_IF_ERROR(ResolveNames(binding->type(), impl_scope)); - RETURN_IF_ERROR(impl_scope.Add(binding->name(), binding)); + CARBON_RETURN_IF_ERROR(ResolveNames(binding->type(), impl_scope)); + CARBON_RETURN_IF_ERROR(impl_scope.Add(binding->name(), binding)); } - RETURN_IF_ERROR(ResolveNames(*impl.impl_type(), impl_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(*impl.impl_type(), impl_scope)); // Only add `Self` to the impl_scope if it is not already in the enclosing // scope. Add `Self` after we resolve names for the impl_type, so you // can't write something like `impl Vector(Self) as ...`. Add `Self` // before resolving names in the interface, so you can write something // like `impl VeryLongTypeName as AddWith(Self)` if (!enclosing_scope.Resolve("Self", impl.source_loc()).ok()) { - RETURN_IF_ERROR(AddExposedNames(*impl.self(), impl_scope)); + CARBON_RETURN_IF_ERROR(AddExposedNames(*impl.self(), impl_scope)); } - RETURN_IF_ERROR(ResolveNames(impl.interface(), impl_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(impl.interface(), impl_scope)); for (Nonnull member : impl.members()) { - RETURN_IF_ERROR(AddExposedNames(*member, impl_scope)); + CARBON_RETURN_IF_ERROR(AddExposedNames(*member, impl_scope)); } for (Nonnull member : impl.members()) { - RETURN_IF_ERROR(ResolveNames(*member, impl_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(*member, impl_scope)); } break; } @@ -352,19 +368,21 @@ static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope) StaticScope function_scope; function_scope.AddParent(&enclosing_scope); for (Nonnull binding : function.deduced_parameters()) { - RETURN_IF_ERROR(ResolveNames(binding->type(), function_scope)); - RETURN_IF_ERROR(function_scope.Add(binding->name(), binding)); + CARBON_RETURN_IF_ERROR(ResolveNames(binding->type(), function_scope)); + CARBON_RETURN_IF_ERROR(function_scope.Add(binding->name(), binding)); } if (function.is_method()) { - RETURN_IF_ERROR(ResolveNames(function.me_pattern(), function_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(function.me_pattern(), function_scope)); } - RETURN_IF_ERROR(ResolveNames(function.param_pattern(), function_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(function.param_pattern(), function_scope)); if (function.return_term().type_expression().has_value()) { - RETURN_IF_ERROR(ResolveNames(**function.return_term().type_expression(), - function_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames( + **function.return_term().type_expression(), function_scope)); } if (function.body().has_value()) { - RETURN_IF_ERROR(ResolveNames(**function.body(), function_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(**function.body(), function_scope)); } break; } @@ -372,10 +390,11 @@ static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope) auto& class_decl = cast(declaration); StaticScope class_scope; class_scope.AddParent(&enclosing_scope); - RETURN_IF_ERROR(class_scope.Add(class_decl.name(), &class_decl)); - RETURN_IF_ERROR(AddExposedNames(*class_decl.self(), class_scope)); + CARBON_RETURN_IF_ERROR(class_scope.Add(class_decl.name(), &class_decl)); + CARBON_RETURN_IF_ERROR(AddExposedNames(*class_decl.self(), class_scope)); if (class_decl.type_params().has_value()) { - RETURN_IF_ERROR(ResolveNames(**class_decl.type_params(), class_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(**class_decl.type_params(), class_scope)); } // TODO: Disable unqualified access of members by other members for now. @@ -386,7 +405,7 @@ static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope) // AddExposedNames(*member, class_scope); // } for (Nonnull member : class_decl.members()) { - RETURN_IF_ERROR(ResolveNames(*member, class_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(*member, class_scope)); } break; } @@ -397,7 +416,7 @@ static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope) // need to check for duplicates. std::set alternative_names; for (Nonnull alternative : choice.alternatives()) { - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ResolveNames(alternative->signature(), enclosing_scope)); if (!alternative_names.insert(alternative->name()).second) { return CompilationError(alternative->source_loc()) @@ -409,15 +428,16 @@ static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope) } case DeclarationKind::VariableDeclaration: { auto& var = cast(declaration); - RETURN_IF_ERROR(ResolveNames(var.binding(), enclosing_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(var.binding(), enclosing_scope)); if (var.has_initializer()) { - RETURN_IF_ERROR(ResolveNames(var.initializer(), enclosing_scope)); + CARBON_RETURN_IF_ERROR( + ResolveNames(var.initializer(), enclosing_scope)); } break; } case DeclarationKind::SelfDeclaration: { - FATAL() << "Unreachable: resolving names for `Self` declaration"; + CARBON_FATAL() << "Unreachable: resolving names for `Self` declaration"; } } return Success(); @@ -426,10 +446,10 @@ static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope) auto ResolveNames(AST& ast) -> ErrorOr { StaticScope file_scope; for (auto declaration : ast.declarations) { - RETURN_IF_ERROR(AddExposedNames(*declaration, file_scope)); + CARBON_RETURN_IF_ERROR(AddExposedNames(*declaration, file_scope)); } for (auto declaration : ast.declarations) { - RETURN_IF_ERROR(ResolveNames(*declaration, file_scope)); + CARBON_RETURN_IF_ERROR(ResolveNames(*declaration, file_scope)); } return ResolveNames(**ast.main_call, file_scope); } diff --git a/explorer/interpreter/stack.h b/explorer/interpreter/stack.h index 2acf2fc13986..f91e24f840a4 100644 --- a/explorer/interpreter/stack.h +++ b/explorer/interpreter/stack.h @@ -32,7 +32,7 @@ struct Stack { // // - Requires: !this->IsEmpty() auto Pop() -> T { - CHECK(!IsEmpty()) << "Can't pop from empty stack."; + CARBON_CHECK(!IsEmpty()) << "Can't pop from empty stack."; auto r = std::move(elements_.back()); elements_.pop_back(); return r; @@ -42,8 +42,8 @@ struct Stack { // // - Requires: n >= 0 && n <= Count() void Pop(int n) { - CHECK(n >= 0) << "Negative pop count disallowed."; - CHECK(static_cast(n) <= elements_.size()) + CARBON_CHECK(n >= 0) << "Negative pop count disallowed."; + CARBON_CHECK(static_cast(n) <= elements_.size()) << "Can only pop as many elements as stack has."; elements_.erase(elements_.end() - n, elements_.end()); } @@ -52,7 +52,7 @@ struct Stack { // // - Requires: !this->IsEmpty() auto Top() const -> const T& { - CHECK(!IsEmpty()) << "Empty stack has no Top()."; + CARBON_CHECK(!IsEmpty()) << "Empty stack has no Top()."; return elements_.back(); } diff --git a/explorer/interpreter/type_checker.cpp b/explorer/interpreter/type_checker.cpp index 5b9fbe2fbe59..1245801e58e7 100644 --- a/explorer/interpreter/type_checker.cpp +++ b/explorer/interpreter/type_checker.cpp @@ -227,8 +227,8 @@ auto TypeChecker::FieldTypes(const NominalClassType& class_type) const auto TypeChecker::IsImplicitlyConvertible( Nonnull source, Nonnull destination) const -> bool { - CHECK(IsConcreteType(source)); - CHECK(IsConcreteType(destination)); + CARBON_CHECK(IsConcreteType(source)); + CARBON_CHECK(IsConcreteType(destination)); if (TypeEqual(source, destination)) { return true; } @@ -328,12 +328,12 @@ auto TypeChecker::ArgumentDeduction( if (!success) { // Variable already has a match. // TODO: can we allow implicit conversions here? - RETURN_IF_ERROR(ExpectExactType(source_loc, "argument deduction", - it->second, arg_type)); + CARBON_RETURN_IF_ERROR(ExpectExactType( + source_loc, "argument deduction", it->second, arg_type)); } } else { - RETURN_IF_ERROR(ExpectExactType(source_loc, "argument deduction", - param_type, arg_type)); + CARBON_RETURN_IF_ERROR(ExpectExactType(source_loc, "argument deduction", + param_type, arg_type)); } return Success(); } @@ -353,9 +353,9 @@ auto TypeChecker::ArgumentDeduction( << arg_tup.elements().size(); } for (size_t i = 0; i < param_tup.elements().size(); ++i) { - RETURN_IF_ERROR(ArgumentDeduction(source_loc, type_params, deduced, - param_tup.elements()[i], - arg_tup.elements()[i])); + CARBON_RETURN_IF_ERROR( + ArgumentDeduction(source_loc, type_params, deduced, + param_tup.elements()[i], arg_tup.elements()[i])); } return Success(); } @@ -380,9 +380,9 @@ auto TypeChecker::ArgumentDeduction( << "mismatch in field names, " << param_struct.fields()[i].name << " != " << arg_struct.fields()[i].name; } - RETURN_IF_ERROR(ArgumentDeduction(source_loc, type_params, deduced, - param_struct.fields()[i].value, - arg_struct.fields()[i].value)); + CARBON_RETURN_IF_ERROR(ArgumentDeduction( + source_loc, type_params, deduced, param_struct.fields()[i].value, + arg_struct.fields()[i].value)); } return Success(); } @@ -396,12 +396,12 @@ auto TypeChecker::ArgumentDeduction( const auto& param_fn = cast(*param_type); const auto& arg_fn = cast(*arg_type); // TODO: handle situation when arg has deduced parameters. - RETURN_IF_ERROR(ArgumentDeduction(source_loc, type_params, deduced, - ¶m_fn.parameters(), - &arg_fn.parameters())); - RETURN_IF_ERROR(ArgumentDeduction(source_loc, type_params, deduced, - ¶m_fn.return_type(), - &arg_fn.return_type())); + CARBON_RETURN_IF_ERROR(ArgumentDeduction(source_loc, type_params, deduced, + ¶m_fn.parameters(), + &arg_fn.parameters())); + CARBON_RETURN_IF_ERROR(ArgumentDeduction(source_loc, type_params, deduced, + ¶m_fn.return_type(), + &arg_fn.return_type())); return Success(); } case Value::Kind::PointerType: { @@ -426,7 +426,7 @@ auto TypeChecker::ArgumentDeduction( if (param_class_type.declaration().name() == arg_class_type.declaration().name()) { for (const auto& [ty, param_ty] : param_class_type.type_args()) { - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ArgumentDeduction(source_loc, type_params, deduced, param_ty, arg_class_type.type_args().at(ty))); } @@ -468,8 +468,8 @@ auto TypeChecker::ArgumentDeduction( case Value::Kind::AlternativeConstructorValue: case Value::Kind::ContinuationValue: case Value::Kind::StringValue: - FATAL() << "In ArgumentDeduction: expected type, not value " - << *param_type; + CARBON_FATAL() << "In ArgumentDeduction: expected type, not value " + << *param_type; } } @@ -569,7 +569,7 @@ auto TypeChecker::Substitute( case Value::Kind::AlternativeConstructorValue: case Value::Kind::ContinuationValue: case Value::Kind::StringValue: - FATAL() << "In Substitute: expected type, not value " << *type; + CARBON_FATAL() << "In Substitute: expected type, not value " << *type; } } @@ -639,10 +639,11 @@ auto TypeChecker::SatisfyImpls( for (Nonnull impl_binding : impl_bindings) { Nonnull interface = Substitute(deduced_type_args, impl_binding->interface()); - ASSIGN_OR_RETURN(Nonnull impl, - impl_scope.Resolve( - interface, deduced_type_args[impl_binding->type_var()], - source_loc, *this)); + CARBON_ASSIGN_OR_RETURN( + Nonnull impl, + impl_scope.Resolve(interface, + deduced_type_args[impl_binding->type_var()], + source_loc, *this)); impls.emplace(impl_binding, impl); } return Success(); @@ -659,21 +660,23 @@ auto TypeChecker::TypeCheckExp(Nonnull e, } switch (e->kind()) { case ExpressionKind::InstantiateImpl: { - FATAL() << "instantiate impl nodes are generated during type checking"; + CARBON_FATAL() + << "instantiate impl nodes are generated during type checking"; } case ExpressionKind::IndexExpression: { auto& index = cast(*e); - RETURN_IF_ERROR(TypeCheckExp(&index.aggregate(), impl_scope)); - RETURN_IF_ERROR(TypeCheckExp(&index.offset(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&index.aggregate(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&index.offset(), impl_scope)); const Value& aggregate_type = index.aggregate().static_type(); switch (aggregate_type.kind()) { case Value::Kind::TupleValue: { const auto& tuple_type = cast(aggregate_type); - RETURN_IF_ERROR(ExpectExactType(index.offset().source_loc(), - "tuple index", arena_->New(), - &index.offset().static_type())); - ASSIGN_OR_RETURN(auto offset_value, - InterpExp(&index.offset(), arena_, trace_stream_)); + CARBON_RETURN_IF_ERROR(ExpectExactType( + index.offset().source_loc(), "tuple index", + arena_->New(), &index.offset().static_type())); + CARBON_ASSIGN_OR_RETURN( + auto offset_value, + InterpExp(&index.offset(), arena_, trace_stream_)); int i = cast(*offset_value).value(); if (i < 0 || i >= static_cast(tuple_type.elements().size())) { return CompilationError(e->source_loc()) @@ -685,9 +688,9 @@ auto TypeChecker::TypeCheckExp(Nonnull e, return Success(); } case Value::Kind::StaticArrayType: { - RETURN_IF_ERROR(ExpectExactType(index.offset().source_loc(), - "array index", arena_->New(), - &index.offset().static_type())); + CARBON_RETURN_IF_ERROR(ExpectExactType( + index.offset().source_loc(), "array index", + arena_->New(), &index.offset().static_type())); index.set_static_type( &cast(aggregate_type).element_type()); index.set_value_category(index.aggregate().value_category()); @@ -700,7 +703,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, case ExpressionKind::TupleLiteral: { std::vector> arg_types; for (auto& arg : cast(*e).fields()) { - RETURN_IF_ERROR(TypeCheckExp(arg, impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(arg, impl_scope)); arg_types.push_back(&arg->static_type()); } e->set_static_type(arena_->New(std::move(arg_types))); @@ -710,7 +713,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, case ExpressionKind::StructLiteral: { std::vector arg_types; for (auto& arg : cast(*e).fields()) { - RETURN_IF_ERROR(TypeCheckExp(&arg.expression(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&arg.expression(), impl_scope)); arg_types.push_back({arg.name(), &arg.expression().static_type()}); } e->set_static_type(arena_->New(std::move(arg_types))); @@ -720,10 +723,10 @@ auto TypeChecker::TypeCheckExp(Nonnull e, case ExpressionKind::StructTypeLiteral: { auto& struct_type = cast(*e); for (auto& arg : struct_type.fields()) { - RETURN_IF_ERROR(TypeCheckExp(&arg.expression(), impl_scope)); - ASSIGN_OR_RETURN(auto value, - InterpExp(&arg.expression(), arena_, trace_stream_)); - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR(TypeCheckExp(&arg.expression(), impl_scope)); + CARBON_ASSIGN_OR_RETURN( + auto value, InterpExp(&arg.expression(), arena_, trace_stream_)); + CARBON_RETURN_IF_ERROR( ExpectIsConcreteType(arg.expression().source_loc(), value)); } if (struct_type.fields().empty()) { @@ -740,7 +743,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, } case ExpressionKind::FieldAccessExpression: { auto& access = cast(*e); - RETURN_IF_ERROR(TypeCheckExp(&access.aggregate(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&access.aggregate(), impl_scope)); const Value& aggregate_type = access.aggregate().static_type(); switch (aggregate_type.kind()) { case Value::Kind::StructType: { @@ -772,8 +775,8 @@ auto TypeChecker::TypeCheckExp(Nonnull e, access.set_value_category(ValueCategory::Let); break; default: - FATAL() << "member " << access.field() - << " is not a field or method"; + CARBON_FATAL() << "member " << access.field() + << " is not a field or method"; break; } return Success(); @@ -848,7 +851,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, Nonnull inst_member_type = Substitute(binding_map, &member_type); access.set_static_type(inst_member_type); - CHECK(var_type.binding().impl_binding().has_value()); + CARBON_CHECK(var_type.binding().impl_binding().has_value()); access.set_impl(*var_type.binding().impl_binding()); return Success(); } else { @@ -870,7 +873,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, // If `T` is a type variable and `foo` is a class function in an // interface implemented by `T`, then `T.foo` accesses the `foo` class // function of `T`. - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( Nonnull var_addr, InterpExp(&access.aggregate(), arena_, trace_stream_)); const VariableType& var_type = cast(*var_addr); @@ -885,7 +888,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, Nonnull inst_member_type = Substitute(binding_map, &member_type); access.set_static_type(inst_member_type); - CHECK(var_type.binding().impl_binding().has_value()); + CARBON_CHECK(var_type.binding().impl_binding().has_value()); access.set_impl(*var_type.binding().impl_binding()); return Success(); } else { @@ -908,7 +911,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, const auto& function = cast(ident.value_node().base()); if (!function.has_static_type()) { - CHECK(function.return_term().is_auto()); + CARBON_CHECK(function.return_term().is_auto()); return CompilationError(ident.source_loc()) << "Function calls itself, but has a deduced return type"; } @@ -929,75 +932,81 @@ auto TypeChecker::TypeCheckExp(Nonnull e, auto& op = cast(*e); std::vector> ts; for (Nonnull argument : op.arguments()) { - RETURN_IF_ERROR(TypeCheckExp(argument, impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(argument, impl_scope)); ts.push_back(&argument->static_type()); } switch (op.op()) { case Operator::Neg: - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "negation", - arena_->New(), ts[0])); + CARBON_RETURN_IF_ERROR(ExpectExactType( + e->source_loc(), "negation", arena_->New(), ts[0])); op.set_static_type(arena_->New()); op.set_value_category(ValueCategory::Let); return Success(); case Operator::Add: - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "addition(1)", - arena_->New(), ts[0])); - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "addition(2)", - arena_->New(), ts[1])); + CARBON_RETURN_IF_ERROR(ExpectExactType( + e->source_loc(), "addition(1)", arena_->New(), ts[0])); + CARBON_RETURN_IF_ERROR(ExpectExactType( + e->source_loc(), "addition(2)", arena_->New(), ts[1])); op.set_static_type(arena_->New()); op.set_value_category(ValueCategory::Let); return Success(); case Operator::Sub: - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "subtraction(1)", - arena_->New(), ts[0])); - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "subtraction(2)", - arena_->New(), ts[1])); + CARBON_RETURN_IF_ERROR( + ExpectExactType(e->source_loc(), "subtraction(1)", + arena_->New(), ts[0])); + CARBON_RETURN_IF_ERROR( + ExpectExactType(e->source_loc(), "subtraction(2)", + arena_->New(), ts[1])); op.set_static_type(arena_->New()); op.set_value_category(ValueCategory::Let); return Success(); case Operator::Mul: - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "multiplication(1)", - arena_->New(), ts[0])); - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "multiplication(2)", - arena_->New(), ts[1])); + CARBON_RETURN_IF_ERROR( + ExpectExactType(e->source_loc(), "multiplication(1)", + arena_->New(), ts[0])); + CARBON_RETURN_IF_ERROR( + ExpectExactType(e->source_loc(), "multiplication(2)", + arena_->New(), ts[1])); op.set_static_type(arena_->New()); op.set_value_category(ValueCategory::Let); return Success(); case Operator::And: - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "&&(1)", - arena_->New(), ts[0])); - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "&&(2)", - arena_->New(), ts[1])); + CARBON_RETURN_IF_ERROR(ExpectExactType( + e->source_loc(), "&&(1)", arena_->New(), ts[0])); + CARBON_RETURN_IF_ERROR(ExpectExactType( + e->source_loc(), "&&(2)", arena_->New(), ts[1])); op.set_static_type(arena_->New()); op.set_value_category(ValueCategory::Let); return Success(); case Operator::Or: - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "||(1)", - arena_->New(), ts[0])); - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "||(2)", - arena_->New(), ts[1])); + CARBON_RETURN_IF_ERROR(ExpectExactType( + e->source_loc(), "||(1)", arena_->New(), ts[0])); + CARBON_RETURN_IF_ERROR(ExpectExactType( + e->source_loc(), "||(2)", arena_->New(), ts[1])); op.set_static_type(arena_->New()); op.set_value_category(ValueCategory::Let); return Success(); case Operator::Not: - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "!", - arena_->New(), ts[0])); + CARBON_RETURN_IF_ERROR(ExpectExactType( + e->source_loc(), "!", arena_->New(), ts[0])); op.set_static_type(arena_->New()); op.set_value_category(ValueCategory::Let); return Success(); case Operator::Eq: - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "==", ts[0], ts[1])); + CARBON_RETURN_IF_ERROR( + ExpectExactType(e->source_loc(), "==", ts[0], ts[1])); op.set_static_type(arena_->New()); op.set_value_category(ValueCategory::Let); return Success(); case Operator::Deref: - RETURN_IF_ERROR(ExpectPointerType(e->source_loc(), "*", ts[0])); + CARBON_RETURN_IF_ERROR( + ExpectPointerType(e->source_loc(), "*", ts[0])); op.set_static_type(&cast(*ts[0]).type()); op.set_value_category(ValueCategory::Var); return Success(); case Operator::Ptr: - RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "*", - arena_->New(), ts[0])); + CARBON_RETURN_IF_ERROR(ExpectExactType( + e->source_loc(), "*", arena_->New(), ts[0])); op.set_static_type(arena_->New()); op.set_value_category(ValueCategory::Let); return Success(); @@ -1015,8 +1024,8 @@ auto TypeChecker::TypeCheckExp(Nonnull e, } case ExpressionKind::CallExpression: { auto& call = cast(*e); - RETURN_IF_ERROR(TypeCheckExp(&call.function(), impl_scope)); - RETURN_IF_ERROR(TypeCheckExp(&call.argument(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&call.function(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&call.argument(), impl_scope)); switch (call.function().static_type().kind()) { case Value::Kind::FunctionType: { const auto& fun_t = cast(call.function().static_type()); @@ -1024,9 +1033,9 @@ auto TypeChecker::TypeCheckExp(Nonnull e, Nonnull return_type = &fun_t.return_type(); if (!fun_t.deduced().empty()) { BindingMap deduced_type_args; - RETURN_IF_ERROR(ArgumentDeduction(e->source_loc(), fun_t.deduced(), - deduced_type_args, parameters, - &call.argument().static_type())); + CARBON_RETURN_IF_ERROR(ArgumentDeduction( + e->source_loc(), fun_t.deduced(), deduced_type_args, parameters, + &call.argument().static_type())); call.set_deduced_args(deduced_type_args); for (Nonnull deduced_param : fun_t.deduced()) { @@ -1044,15 +1053,16 @@ auto TypeChecker::TypeCheckExp(Nonnull e, return_type = Substitute(deduced_type_args, return_type); // Find impls for all the impl bindings of the function. ImplExpMap impls; - RETURN_IF_ERROR(SatisfyImpls(fun_t.impl_bindings(), impl_scope, - e->source_loc(), deduced_type_args, - impls)); + CARBON_RETURN_IF_ERROR(SatisfyImpls(fun_t.impl_bindings(), + impl_scope, e->source_loc(), + deduced_type_args, impls)); call.set_impls(impls); } else { // No deduced parameters. Check that the argument types // are convertible to the parameter types. - RETURN_IF_ERROR(ExpectType(e->source_loc(), "call", parameters, - &call.argument().static_type())); + CARBON_RETURN_IF_ERROR(ExpectType(e->source_loc(), "call", + parameters, + &call.argument().static_type())); } call.set_static_type(return_type); call.set_value_category(ValueCategory::Let); @@ -1069,24 +1079,26 @@ auto TypeChecker::TypeCheckExp(Nonnull e, if (trace_stream_) { **trace_stream_ << "pattern matching type params and args\n"; } - RETURN_IF_ERROR(ExpectType(call.source_loc(), "call", - ¶m_name.params().static_type(), - &call.argument().static_type())); - ASSIGN_OR_RETURN(Nonnull arg, - InterpExp(&call.argument(), arena_, trace_stream_)); - CHECK(PatternMatch(¶m_name.params().value(), arg, - call.source_loc(), std::nullopt, generic_args, - trace_stream_)); + CARBON_RETURN_IF_ERROR(ExpectType(call.source_loc(), "call", + ¶m_name.params().static_type(), + &call.argument().static_type())); + CARBON_ASSIGN_OR_RETURN( + Nonnull arg, + InterpExp(&call.argument(), arena_, trace_stream_)); + CARBON_CHECK(PatternMatch(¶m_name.params().value(), arg, + call.source_loc(), std::nullopt, + generic_args, trace_stream_)); // Find impls for all the impl bindings. ImplExpMap impls; for (const auto& [binding, val] : generic_args) { if (binding->impl_binding().has_value()) { Nonnull impl_binding = *binding->impl_binding(); - ASSIGN_OR_RETURN(Nonnull impl, - impl_scope.Resolve(impl_binding->interface(), - generic_args[binding], - call.source_loc(), *this)); + CARBON_ASSIGN_OR_RETURN( + Nonnull impl, + impl_scope.Resolve(impl_binding->interface(), + generic_args[binding], call.source_loc(), + *this)); impls.emplace(impl_binding, impl); } } @@ -1112,7 +1124,8 @@ auto TypeChecker::TypeCheckExp(Nonnull e, break; } default: - FATAL() << "unknown type of ParameterizedEntityName for " << decl; + CARBON_FATAL() + << "unknown type of ParameterizedEntityName for " << decl; } return Success(); } @@ -1127,13 +1140,15 @@ auto TypeChecker::TypeCheckExp(Nonnull e, } case ExpressionKind::FunctionTypeLiteral: { auto& fn = cast(*e); - ASSIGN_OR_RETURN(Nonnull param_type, - InterpExp(&fn.parameter(), arena_, trace_stream_)); - RETURN_IF_ERROR( + CARBON_ASSIGN_OR_RETURN( + Nonnull param_type, + InterpExp(&fn.parameter(), arena_, trace_stream_)); + CARBON_RETURN_IF_ERROR( ExpectIsConcreteType(fn.parameter().source_loc(), param_type)); - ASSIGN_OR_RETURN(Nonnull ret_type, - InterpExp(&fn.return_type(), arena_, trace_stream_)); - RETURN_IF_ERROR( + CARBON_ASSIGN_OR_RETURN( + Nonnull ret_type, + InterpExp(&fn.return_type(), arena_, trace_stream_)); + CARBON_RETURN_IF_ERROR( ExpectIsConcreteType(fn.return_type().source_loc(), ret_type)); fn.set_static_type(arena_->New()); fn.set_value_category(ValueCategory::Let); @@ -1145,14 +1160,14 @@ auto TypeChecker::TypeCheckExp(Nonnull e, return Success(); case ExpressionKind::IntrinsicExpression: { auto& intrinsic_exp = cast(*e); - RETURN_IF_ERROR(TypeCheckExp(&intrinsic_exp.args(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&intrinsic_exp.args(), impl_scope)); switch (cast(*e).intrinsic()) { case IntrinsicExpression::Intrinsic::Print: if (intrinsic_exp.args().fields().size() != 1) { return CompilationError(e->source_loc()) << "__intrinsic_print takes 1 argument"; } - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ExpectType(e->source_loc(), "__intrinsic_print argument", arena_->New(), &intrinsic_exp.args().fields()[0]->static_type())); @@ -1171,15 +1186,17 @@ auto TypeChecker::TypeCheckExp(Nonnull e, return Success(); case ExpressionKind::IfExpression: { auto& if_expr = cast(*e); - RETURN_IF_ERROR(TypeCheckExp(&if_expr.condition(), impl_scope)); - RETURN_IF_ERROR(ExpectType(if_expr.source_loc(), "condition of `if`", - arena_->New(), - &if_expr.condition().static_type())); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&if_expr.condition(), impl_scope)); + CARBON_RETURN_IF_ERROR(ExpectType( + if_expr.source_loc(), "condition of `if`", arena_->New(), + &if_expr.condition().static_type())); // TODO: Compute the common type and convert both operands to it. - RETURN_IF_ERROR(TypeCheckExp(&if_expr.then_expression(), impl_scope)); - RETURN_IF_ERROR(TypeCheckExp(&if_expr.else_expression(), impl_scope)); - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( + TypeCheckExp(&if_expr.then_expression(), impl_scope)); + CARBON_RETURN_IF_ERROR( + TypeCheckExp(&if_expr.else_expression(), impl_scope)); + CARBON_RETURN_IF_ERROR( ExpectExactType(e->source_loc(), "expression of `if` expression", &if_expr.then_expression().static_type(), &if_expr.else_expression().static_type())); @@ -1188,24 +1205,25 @@ auto TypeChecker::TypeCheckExp(Nonnull e, return Success(); } case ExpressionKind::UnimplementedExpression: - FATAL() << "Unimplemented: " << *e; + CARBON_FATAL() << "Unimplemented: " << *e; case ExpressionKind::ArrayTypeLiteral: { auto& array_literal = cast(*e); - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( TypeCheckExp(&array_literal.element_type_expression(), impl_scope)); - ASSIGN_OR_RETURN(Nonnull element_type, - InterpExp(&array_literal.element_type_expression(), - arena_, trace_stream_)); - RETURN_IF_ERROR(ExpectIsConcreteType( + CARBON_ASSIGN_OR_RETURN( + Nonnull element_type, + InterpExp(&array_literal.element_type_expression(), arena_, + trace_stream_)); + CARBON_RETURN_IF_ERROR(ExpectIsConcreteType( array_literal.element_type_expression().source_loc(), element_type)); - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( TypeCheckExp(&array_literal.size_expression(), impl_scope)); - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ExpectExactType(array_literal.size_expression().source_loc(), "array size", arena_->New(), &array_literal.size_expression().static_type())); - ASSIGN_OR_RETURN( + CARBON_ASSIGN_OR_RETURN( Nonnull size_value, InterpExp(&array_literal.size_expression(), arena_, trace_stream_)); if (cast(size_value)->value() < 0) { @@ -1271,7 +1289,7 @@ void TypeChecker::BringImplsIntoScope( void TypeChecker::BringImplIntoScope(Nonnull impl_binding, ImplScope& impl_scope) { - CHECK(impl_binding->type_var()->symbolic_identity().has_value()); + CARBON_CHECK(impl_binding->type_var()->symbolic_identity().has_value()); auto impl_id = arena_->New(impl_binding->source_loc(), "impl"); impl_id->set_value_node(impl_binding); @@ -1303,14 +1321,15 @@ auto TypeChecker::TypeCheckPattern( return CompilationError(binding.type().source_loc()) << "The type of a binding pattern cannot contain bindings."; } - RETURN_IF_ERROR(TypeCheckPattern(&binding.type(), std::nullopt, - impl_scope, enclosing_value_category)); - ASSIGN_OR_RETURN(Nonnull type, - InterpPattern(&binding.type(), arena_, trace_stream_)); - RETURN_IF_ERROR(ExpectIsType(binding.source_loc(), type)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern( + &binding.type(), std::nullopt, impl_scope, enclosing_value_category)); + CARBON_ASSIGN_OR_RETURN( + Nonnull type, + InterpPattern(&binding.type(), arena_, trace_stream_)); + CARBON_RETURN_IF_ERROR(ExpectIsType(binding.source_loc(), type)); if (expected) { if (IsConcreteType(type)) { - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ExpectType(p->source_loc(), "name binding", type, *expected)); } else { BindingMap generic_args; @@ -1323,10 +1342,10 @@ auto TypeChecker::TypeCheckPattern( type = *expected; } } - RETURN_IF_ERROR(ExpectIsConcreteType(binding.source_loc(), type)); + CARBON_RETURN_IF_ERROR(ExpectIsConcreteType(binding.source_loc(), type)); binding.set_static_type(type); - ASSIGN_OR_RETURN(Nonnull binding_value, - InterpPattern(&binding, arena_, trace_stream_)); + CARBON_ASSIGN_OR_RETURN(Nonnull binding_value, + InterpPattern(&binding, arena_, trace_stream_)); SetValue(&binding, binding_value); if (!binding.has_value_category()) { @@ -1336,9 +1355,10 @@ auto TypeChecker::TypeCheckPattern( } case PatternKind::GenericBinding: { auto& binding = cast(*p); - RETURN_IF_ERROR(TypeCheckExp(&binding.type(), impl_scope)); - ASSIGN_OR_RETURN(Nonnull type, - InterpExp(&binding.type(), arena_, trace_stream_)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&binding.type(), impl_scope)); + CARBON_ASSIGN_OR_RETURN( + Nonnull type, + InterpExp(&binding.type(), arena_, trace_stream_)); if (expected) { return CompilationError(binding.type().source_loc()) << "Generic binding may not occur in pattern with expected " @@ -1346,8 +1366,8 @@ auto TypeChecker::TypeCheckPattern( << binding; } binding.set_static_type(type); - ASSIGN_OR_RETURN(Nonnull val, - InterpPattern(&binding, arena_, trace_stream_)); + CARBON_ASSIGN_OR_RETURN(Nonnull val, + InterpPattern(&binding, arena_, trace_stream_)); binding.set_symbolic_identity(val); SetValue(&binding, val); @@ -1376,29 +1396,30 @@ auto TypeChecker::TypeCheckPattern( if (expected) { expected_field_type = cast(**expected).elements()[i]; } - RETURN_IF_ERROR(TypeCheckPattern(field, expected_field_type, impl_scope, - enclosing_value_category)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern( + field, expected_field_type, impl_scope, enclosing_value_category)); if (trace_stream_) **trace_stream_ << "finished checking tuple pattern field " << *field << "\n"; field_types.push_back(&field->static_type()); } tuple.set_static_type(arena_->New(std::move(field_types))); - ASSIGN_OR_RETURN(Nonnull tuple_value, - InterpPattern(&tuple, arena_, trace_stream_)); + CARBON_ASSIGN_OR_RETURN(Nonnull tuple_value, + InterpPattern(&tuple, arena_, trace_stream_)); SetValue(&tuple, tuple_value); return Success(); } case PatternKind::AlternativePattern: { auto& alternative = cast(*p); - RETURN_IF_ERROR(TypeCheckExp(&alternative.choice_type(), impl_scope)); + CARBON_RETURN_IF_ERROR( + TypeCheckExp(&alternative.choice_type(), impl_scope)); if (alternative.choice_type().static_type().kind() != Value::Kind::TypeOfChoiceType) { return CompilationError(alternative.source_loc()) << "alternative pattern does not name a choice type."; } if (expected) { - RETURN_IF_ERROR(ExpectExactType( + CARBON_RETURN_IF_ERROR(ExpectExactType( alternative.source_loc(), "alternative pattern", *expected, &alternative.choice_type().static_type())); } @@ -1413,33 +1434,35 @@ auto TypeChecker::TypeCheckPattern( << "'" << alternative.alternative_name() << "' is not an alternative of " << choice_type; } - RETURN_IF_ERROR(TypeCheckPattern(&alternative.arguments(), - *parameter_types, impl_scope, - enclosing_value_category)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern(&alternative.arguments(), + *parameter_types, impl_scope, + enclosing_value_category)); alternative.set_static_type(&choice_type); - ASSIGN_OR_RETURN(Nonnull alternative_value, - InterpPattern(&alternative, arena_, trace_stream_)); + CARBON_ASSIGN_OR_RETURN( + Nonnull alternative_value, + InterpPattern(&alternative, arena_, trace_stream_)); SetValue(&alternative, alternative_value); return Success(); } case PatternKind::ExpressionPattern: { auto& expression = cast(*p).expression(); - RETURN_IF_ERROR(TypeCheckExp(&expression, impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&expression, impl_scope)); p->set_static_type(&expression.static_type()); - ASSIGN_OR_RETURN(Nonnull expr_value, - InterpPattern(p, arena_, trace_stream_)); + CARBON_ASSIGN_OR_RETURN(Nonnull expr_value, + InterpPattern(p, arena_, trace_stream_)); SetValue(p, expr_value); return Success(); } case PatternKind::VarPattern: auto& let_var_pattern = cast(*p); - RETURN_IF_ERROR(TypeCheckPattern(&let_var_pattern.pattern(), expected, - impl_scope, - let_var_pattern.value_category())); + CARBON_RETURN_IF_ERROR( + TypeCheckPattern(&let_var_pattern.pattern(), expected, impl_scope, + let_var_pattern.value_category())); let_var_pattern.set_static_type(&let_var_pattern.pattern().static_type()); - ASSIGN_OR_RETURN(Nonnull pattern_value, - InterpPattern(&let_var_pattern, arena_, trace_stream_)); + CARBON_ASSIGN_OR_RETURN( + Nonnull pattern_value, + InterpPattern(&let_var_pattern, arena_, trace_stream_)); SetValue(&let_var_pattern, pattern_value); return Success(); } @@ -1454,25 +1477,26 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, switch (s->kind()) { case StatementKind::Match: { auto& match = cast(*s); - RETURN_IF_ERROR(TypeCheckExp(&match.expression(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&match.expression(), impl_scope)); std::vector new_clauses; for (auto& clause : match.clauses()) { ImplScope clause_scope; clause_scope.AddParent(&impl_scope); - RETURN_IF_ERROR(TypeCheckPattern(&clause.pattern(), - &match.expression().static_type(), - clause_scope, ValueCategory::Let)); - RETURN_IF_ERROR(TypeCheckStmt(&clause.statement(), clause_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern( + &clause.pattern(), &match.expression().static_type(), clause_scope, + ValueCategory::Let)); + CARBON_RETURN_IF_ERROR( + TypeCheckStmt(&clause.statement(), clause_scope)); } return Success(); } case StatementKind::While: { auto& while_stmt = cast(*s); - RETURN_IF_ERROR(TypeCheckExp(&while_stmt.condition(), impl_scope)); - RETURN_IF_ERROR(ExpectType(s->source_loc(), "condition of `while`", - arena_->New(), - &while_stmt.condition().static_type())); - RETURN_IF_ERROR(TypeCheckStmt(&while_stmt.body(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&while_stmt.condition(), impl_scope)); + CARBON_RETURN_IF_ERROR(ExpectType(s->source_loc(), "condition of `while`", + arena_->New(), + &while_stmt.condition().static_type())); + CARBON_RETURN_IF_ERROR(TypeCheckStmt(&while_stmt.body(), impl_scope)); return Success(); } case StatementKind::Break: @@ -1481,13 +1505,13 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, case StatementKind::Block: { auto& block = cast(*s); for (auto* block_statement : block.statements()) { - RETURN_IF_ERROR(TypeCheckStmt(block_statement, impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckStmt(block_statement, impl_scope)); } return Success(); } case StatementKind::VariableDefinition: { auto& var = cast(*s); - RETURN_IF_ERROR(TypeCheckExp(&var.init(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&var.init(), impl_scope)); const Value& rhs_ty = var.init().static_type(); // FIXME: If the pattern contains a binding that implies a new impl is // available, should that remain in scope for as long as its binding? @@ -1498,17 +1522,17 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, // ``` ImplScope var_scope; var_scope.AddParent(&impl_scope); - RETURN_IF_ERROR(TypeCheckPattern(&var.pattern(), &rhs_ty, var_scope, - var.value_category())); + CARBON_RETURN_IF_ERROR(TypeCheckPattern(&var.pattern(), &rhs_ty, + var_scope, var.value_category())); return Success(); } case StatementKind::Assign: { auto& assign = cast(*s); - RETURN_IF_ERROR(TypeCheckExp(&assign.rhs(), impl_scope)); - RETURN_IF_ERROR(TypeCheckExp(&assign.lhs(), impl_scope)); - RETURN_IF_ERROR(ExpectType(s->source_loc(), "assign", - &assign.lhs().static_type(), - &assign.rhs().static_type())); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&assign.rhs(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&assign.lhs(), impl_scope)); + CARBON_RETURN_IF_ERROR(ExpectType(s->source_loc(), "assign", + &assign.lhs().static_type(), + &assign.rhs().static_type())); if (assign.lhs().value_category() != ValueCategory::Var) { return CompilationError(assign.source_loc()) << "Cannot assign to rvalue '" << assign.lhs() << "'"; @@ -1516,47 +1540,48 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, return Success(); } case StatementKind::ExpressionStatement: { - RETURN_IF_ERROR(TypeCheckExp(&cast(*s).expression(), - impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp( + &cast(*s).expression(), impl_scope)); return Success(); } case StatementKind::If: { auto& if_stmt = cast(*s); - RETURN_IF_ERROR(TypeCheckExp(&if_stmt.condition(), impl_scope)); - RETURN_IF_ERROR(ExpectType(s->source_loc(), "condition of `if`", - arena_->New(), - &if_stmt.condition().static_type())); - RETURN_IF_ERROR(TypeCheckStmt(&if_stmt.then_block(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&if_stmt.condition(), impl_scope)); + CARBON_RETURN_IF_ERROR(ExpectType(s->source_loc(), "condition of `if`", + arena_->New(), + &if_stmt.condition().static_type())); + CARBON_RETURN_IF_ERROR(TypeCheckStmt(&if_stmt.then_block(), impl_scope)); if (if_stmt.else_block()) { - RETURN_IF_ERROR(TypeCheckStmt(*if_stmt.else_block(), impl_scope)); + CARBON_RETURN_IF_ERROR( + TypeCheckStmt(*if_stmt.else_block(), impl_scope)); } return Success(); } case StatementKind::Return: { auto& ret = cast(*s); - RETURN_IF_ERROR(TypeCheckExp(&ret.expression(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&ret.expression(), impl_scope)); ReturnTerm& return_term = ret.function().return_term(); if (return_term.is_auto()) { return_term.set_static_type(&ret.expression().static_type()); } else { - RETURN_IF_ERROR(ExpectType(s->source_loc(), "return", - &return_term.static_type(), - &ret.expression().static_type())); + CARBON_RETURN_IF_ERROR(ExpectType(s->source_loc(), "return", + &return_term.static_type(), + &ret.expression().static_type())); } return Success(); } case StatementKind::Continuation: { auto& cont = cast(*s); - RETURN_IF_ERROR(TypeCheckStmt(&cont.body(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckStmt(&cont.body(), impl_scope)); cont.set_static_type(arena_->New()); return Success(); } case StatementKind::Run: { auto& run = cast(*s); - RETURN_IF_ERROR(TypeCheckExp(&run.argument(), impl_scope)); - RETURN_IF_ERROR(ExpectType(s->source_loc(), "argument of `run`", - arena_->New(), - &run.argument().static_type())); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&run.argument(), impl_scope)); + CARBON_RETURN_IF_ERROR(ExpectType(s->source_loc(), "argument of `run`", + arena_->New(), + &run.argument().static_type())); return Success(); } case StatementKind::Await: { @@ -1601,7 +1626,7 @@ auto TypeChecker::ExpectReturnOnAllPaths( } std::vector new_clauses; for (auto& clause : match.clauses()) { - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ExpectReturnOnAllPaths(&clause.statement(), stmt->source_loc())); } return Success(); @@ -1613,16 +1638,16 @@ auto TypeChecker::ExpectReturnOnAllPaths( << "control-flow reaches end of function that provides a `->` " "return type without reaching a return statement"; } - RETURN_IF_ERROR(ExpectReturnOnAllPaths( + CARBON_RETURN_IF_ERROR(ExpectReturnOnAllPaths( block.statements()[block.statements().size() - 1], block.source_loc())); return Success(); } case StatementKind::If: { auto& if_stmt = cast(*stmt); - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ExpectReturnOnAllPaths(&if_stmt.then_block(), stmt->source_loc())); - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ExpectReturnOnAllPaths(if_stmt.else_block(), stmt->source_loc())); return Success(); } @@ -1657,19 +1682,19 @@ auto TypeChecker::DeclareFunctionDeclaration(Nonnull f, std::vector> impl_bindings; // Bring the deduced parameters into scope. for (Nonnull deduced : f->deduced_parameters()) { - RETURN_IF_ERROR(TypeCheckPattern(deduced, std::nullopt, function_scope, - ValueCategory::Let)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern( + deduced, std::nullopt, function_scope, ValueCategory::Let)); CollectImplBindingsInPattern(deduced, impl_bindings); } // Type check the receiver pattern. if (f->is_method()) { - RETURN_IF_ERROR(TypeCheckPattern(&f->me_pattern(), std::nullopt, - function_scope, ValueCategory::Let)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern( + &f->me_pattern(), std::nullopt, function_scope, ValueCategory::Let)); CollectImplBindingsInPattern(&f->me_pattern(), impl_bindings); } // Type check the parameter pattern. - RETURN_IF_ERROR(TypeCheckPattern(&f->param_pattern(), std::nullopt, - function_scope, ValueCategory::Let)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern(&f->param_pattern(), std::nullopt, + function_scope, ValueCategory::Let)); CollectImplBindingsInPattern(&f->param_pattern(), impl_bindings); // Evaluate the return type, if we can do so without examining the body. @@ -1678,12 +1703,13 @@ auto TypeChecker::DeclareFunctionDeclaration(Nonnull f, return_expression.has_value()) { // We ignore the return value because return type expressions can't bring // new types into scope. - RETURN_IF_ERROR(TypeCheckExp(*return_expression, function_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(*return_expression, function_scope)); // Should we be doing SetConstantValue instead? -Jeremy // And shouldn't the type of this be Type? - ASSIGN_OR_RETURN(Nonnull ret_type, - InterpExp(*return_expression, arena_, trace_stream_)); - RETURN_IF_ERROR(ExpectIsType(f->source_loc(), ret_type)); + CARBON_ASSIGN_OR_RETURN( + Nonnull ret_type, + InterpExp(*return_expression, arena_, trace_stream_)); + CARBON_RETURN_IF_ERROR(ExpectIsType(f->source_loc(), ret_type)); f->return_term().set_static_type(ret_type); } else if (f->return_term().is_omitted()) { f->return_term().set_static_type(TupleValue::Empty()); @@ -1693,13 +1719,14 @@ auto TypeChecker::DeclareFunctionDeclaration(Nonnull f, return CompilationError(f->return_term().source_loc()) << "Function declaration has deduced return type but no body"; } - RETURN_IF_ERROR(TypeCheckStmt(*f->body(), function_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckStmt(*f->body(), function_scope)); if (!f->return_term().is_omitted()) { - RETURN_IF_ERROR(ExpectReturnOnAllPaths(f->body(), f->source_loc())); + CARBON_RETURN_IF_ERROR( + ExpectReturnOnAllPaths(f->body(), f->source_loc())); } } - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( ExpectIsConcreteType(f->source_loc(), &f->return_term().static_type())); f->set_static_type(arena_->New( f->deduced_parameters(), &f->param_pattern().static_type(), @@ -1711,7 +1738,7 @@ auto TypeChecker::DeclareFunctionDeclaration(Nonnull f, return CompilationError(f->return_term().source_loc()) << "`Main` must have an explicit return type"; } - RETURN_IF_ERROR(ExpectExactType( + CARBON_RETURN_IF_ERROR(ExpectExactType( f->return_term().source_loc(), "return type of `Main`", arena_->New(), &f->return_term().static_type())); // TODO: Check that main doesn't have any parameters. @@ -1740,9 +1767,10 @@ auto TypeChecker::TypeCheckFunctionDeclaration(Nonnull f, function_scope); if (trace_stream_) **trace_stream_ << function_scope; - RETURN_IF_ERROR(TypeCheckStmt(*f->body(), function_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckStmt(*f->body(), function_scope)); if (!f->return_term().is_omitted()) { - RETURN_IF_ERROR(ExpectReturnOnAllPaths(f->body(), f->source_loc())); + CARBON_RETURN_IF_ERROR( + ExpectReturnOnAllPaths(f->body(), f->source_loc())); } } if (trace_stream_) { @@ -1762,8 +1790,8 @@ auto TypeChecker::DeclareClassDeclaration(Nonnull class_decl, Nonnull type_params = *class_decl->type_params(); ImplScope class_scope; class_scope.AddParent(&enclosing_scope); - RETURN_IF_ERROR(TypeCheckPattern(type_params, std::nullopt, class_scope, - ValueCategory::Let)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern(type_params, std::nullopt, + class_scope, ValueCategory::Let)); if (trace_stream_) { **trace_stream_ << class_scope; } @@ -1783,7 +1811,7 @@ auto TypeChecker::DeclareClassDeclaration(Nonnull class_decl, // list only contains generic bindings. We need to decide if Carbon should // allow expressions like: // class B((T:! Type, U:! Type), V:! Type) {} - CHECK(field->kind() == PatternKind::GenericBinding); + CARBON_CHECK(field->kind() == PatternKind::GenericBinding); auto& binding = cast(*field); // binding.symbolic_identity() set by call to `TypeCheckPattern(...)` // above. @@ -1797,7 +1825,7 @@ auto TypeChecker::DeclareClassDeclaration(Nonnull class_decl, self->set_static_type(arena_->New(self_type)); for (Nonnull m : class_decl->members()) { - RETURN_IF_ERROR(DeclareDeclaration(m, class_scope)); + CARBON_RETURN_IF_ERROR(DeclareDeclaration(m, class_scope)); } } else { // The declarations of the members may refer to the class, so we @@ -1815,7 +1843,7 @@ auto TypeChecker::DeclareClassDeclaration(Nonnull class_decl, self->set_static_type(static_type); for (Nonnull m : class_decl->members()) { - RETURN_IF_ERROR(DeclareDeclaration(m, enclosing_scope)); + CARBON_RETURN_IF_ERROR(DeclareDeclaration(m, enclosing_scope)); } } if (trace_stream_) { @@ -1840,7 +1868,7 @@ auto TypeChecker::TypeCheckClassDeclaration( **trace_stream_ << class_scope; } for (Nonnull m : class_decl->members()) { - RETURN_IF_ERROR(TypeCheckDeclaration(m, class_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckDeclaration(m, class_scope)); } if (trace_stream_) { **trace_stream_ << "** finished checking class " << class_decl->name() @@ -1859,8 +1887,8 @@ auto TypeChecker::DeclareInterfaceDeclaration( iface_scope.AddParent(&enclosing_scope); if (iface_decl->params().has_value()) { - RETURN_IF_ERROR(TypeCheckPattern(*iface_decl->params(), std::nullopt, - iface_scope, ValueCategory::Let)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern(*iface_decl->params(), std::nullopt, + iface_scope, ValueCategory::Let)); if (trace_stream_) { **trace_stream_ << iface_scope; } @@ -1877,10 +1905,10 @@ auto TypeChecker::DeclareInterfaceDeclaration( } // Process the Self parameter. - RETURN_IF_ERROR(TypeCheckPattern(iface_decl->self(), std::nullopt, - iface_scope, ValueCategory::Let)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern(iface_decl->self(), std::nullopt, + iface_scope, ValueCategory::Let)); for (Nonnull m : iface_decl->members()) { - RETURN_IF_ERROR(DeclareDeclaration(m, iface_scope)); + CARBON_RETURN_IF_ERROR(DeclareDeclaration(m, iface_scope)); } if (trace_stream_) { **trace_stream_ << "** finished declaring interface " << iface_decl->name() @@ -1904,7 +1932,7 @@ auto TypeChecker::TypeCheckInterfaceDeclaration( **trace_stream_ << iface_scope; } for (Nonnull m : iface_decl->members()) { - RETURN_IF_ERROR(TypeCheckDeclaration(m, iface_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckDeclaration(m, iface_scope)); } if (trace_stream_) { **trace_stream_ << "** finished checking interface " << iface_decl->name() @@ -1925,16 +1953,17 @@ auto TypeChecker::DeclareImplDeclaration(Nonnull impl_decl, // Bring the deduced parameters into scope. for (Nonnull deduced : impl_decl->deduced_parameters()) { - RETURN_IF_ERROR(TypeCheckPattern(deduced, std::nullopt, impl_scope, - ValueCategory::Let)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern(deduced, std::nullopt, impl_scope, + ValueCategory::Let)); CollectImplBindingsInPattern(deduced, impl_bindings); } impl_decl->set_impl_bindings(impl_bindings); // Check and interpret the impl_type - RETURN_IF_ERROR(TypeCheckExp(impl_decl->impl_type(), impl_scope)); - ASSIGN_OR_RETURN(Nonnull impl_type_value, - InterpExp(impl_decl->impl_type(), arena_, trace_stream_)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(impl_decl->impl_type(), impl_scope)); + CARBON_ASSIGN_OR_RETURN( + Nonnull impl_type_value, + InterpExp(impl_decl->impl_type(), arena_, trace_stream_)); // Set `Self` to `impl_type`. We do this whether or not it `Self` resolves to // it or the `Self` from an enclosing scope. This needs to be done before @@ -1945,9 +1974,11 @@ auto TypeChecker::DeclareImplDeclaration(Nonnull impl_decl, self->set_static_type(&impl_decl->impl_type()->static_type()); // Check and interpret the interface. - RETURN_IF_ERROR(TypeCheckExp(&impl_decl->interface(), enclosing_scope)); - ASSIGN_OR_RETURN(Nonnull written_iface_type, - InterpExp(&impl_decl->interface(), arena_, trace_stream_)); + CARBON_RETURN_IF_ERROR( + TypeCheckExp(&impl_decl->interface(), enclosing_scope)); + CARBON_ASSIGN_OR_RETURN( + Nonnull written_iface_type, + InterpExp(&impl_decl->interface(), arena_, trace_stream_)); const auto* iface_type = dyn_cast(written_iface_type); if (!iface_type) { @@ -1968,7 +1999,7 @@ auto TypeChecker::DeclareImplDeclaration(Nonnull impl_decl, // Declare the impl members. for (Nonnull m : impl_decl->members()) { - RETURN_IF_ERROR(DeclareDeclaration(m, impl_scope)); + CARBON_RETURN_IF_ERROR(DeclareDeclaration(m, impl_scope)); } // Check that the interface is satisfied by the impl members. for (Nonnull m : iface_decl.members()) { @@ -1981,9 +2012,9 @@ auto TypeChecker::DeclareImplDeclaration(Nonnull impl_decl, binding_map[iface_decl.self()] = impl_type_value; Nonnull iface_mem_type = Substitute(binding_map, &m->static_type()); - RETURN_IF_ERROR(ExpectType((*mem)->source_loc(), - "member of implementation", iface_mem_type, - &(*mem)->static_type())); + CARBON_RETURN_IF_ERROR( + ExpectType((*mem)->source_loc(), "member of implementation", + iface_mem_type, &(*mem)->static_type())); } else { return CompilationError(impl_decl->source_loc()) << "implementation missing " << *mem_name; @@ -2009,7 +2040,7 @@ auto TypeChecker::TypeCheckImplDeclaration(Nonnull impl_decl, impl_scope.AddParent(&enclosing_scope); BringImplsIntoScope(impl_decl->impl_bindings(), impl_scope); for (Nonnull m : impl_decl->members()) { - RETURN_IF_ERROR(TypeCheckDeclaration(m, impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckDeclaration(m, impl_scope)); } if (trace_stream_) { **trace_stream_ << "finished checking impl\n"; @@ -2022,9 +2053,10 @@ auto TypeChecker::DeclareChoiceDeclaration(Nonnull choice, -> ErrorOr { std::vector alternatives; for (Nonnull alternative : choice->alternatives()) { - RETURN_IF_ERROR(TypeCheckExp(&alternative->signature(), enclosing_scope)); - ASSIGN_OR_RETURN(auto signature, InterpExp(&alternative->signature(), - arena_, trace_stream_)); + CARBON_RETURN_IF_ERROR( + TypeCheckExp(&alternative->signature(), enclosing_scope)); + CARBON_ASSIGN_OR_RETURN(auto signature, InterpExp(&alternative->signature(), + arena_, trace_stream_)); alternatives.push_back({.name = alternative->name(), .value = signature}); } auto ct = arena_->New(choice->name(), std::move(alternatives)); @@ -2043,12 +2075,12 @@ auto TypeChecker::TypeCheckChoiceDeclaration( auto TypeChecker::TypeCheck(AST& ast) -> ErrorOr { ImplScope impl_scope; for (Nonnull declaration : ast.declarations) { - RETURN_IF_ERROR(DeclareDeclaration(declaration, impl_scope)); + CARBON_RETURN_IF_ERROR(DeclareDeclaration(declaration, impl_scope)); } for (Nonnull decl : ast.declarations) { - RETURN_IF_ERROR(TypeCheckDeclaration(decl, impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckDeclaration(decl, impl_scope)); } - RETURN_IF_ERROR(TypeCheckExp(*ast.main_call, impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(*ast.main_call, impl_scope)); return Success(); } @@ -2057,25 +2089,25 @@ auto TypeChecker::TypeCheckDeclaration(Nonnull d, -> ErrorOr { switch (d->kind()) { case DeclarationKind::InterfaceDeclaration: { - RETURN_IF_ERROR(TypeCheckInterfaceDeclaration( + CARBON_RETURN_IF_ERROR(TypeCheckInterfaceDeclaration( &cast(*d), impl_scope)); break; } case DeclarationKind::ImplDeclaration: { - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( TypeCheckImplDeclaration(&cast(*d), impl_scope)); break; } case DeclarationKind::FunctionDeclaration: - RETURN_IF_ERROR(TypeCheckFunctionDeclaration( + CARBON_RETURN_IF_ERROR(TypeCheckFunctionDeclaration( &cast(*d), impl_scope)); return Success(); case DeclarationKind::ClassDeclaration: - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( TypeCheckClassDeclaration(&cast(*d), impl_scope)); return Success(); case DeclarationKind::ChoiceDeclaration: - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( TypeCheckChoiceDeclaration(&cast(*d), impl_scope)); return Success(); case DeclarationKind::VariableDeclaration: { @@ -2084,7 +2116,7 @@ auto TypeChecker::TypeCheckDeclaration(Nonnull d, // the declared type of the variable, otherwise returns this // declaration with annotated types. if (var.has_initializer()) { - RETURN_IF_ERROR(TypeCheckExp(&var.initializer(), impl_scope)); + CARBON_RETURN_IF_ERROR(TypeCheckExp(&var.initializer(), impl_scope)); } const auto* binding_type = dyn_cast(&var.binding().type()); @@ -2094,14 +2126,14 @@ auto TypeChecker::TypeCheckDeclaration(Nonnull d, << "Type of a top-level variable must be an expression."; } if (var.has_initializer()) { - RETURN_IF_ERROR(ExpectType(var.source_loc(), "initializer of variable", - &var.static_type(), - &var.initializer().static_type())); + CARBON_RETURN_IF_ERROR( + ExpectType(var.source_loc(), "initializer of variable", + &var.static_type(), &var.initializer().static_type())); } return Success(); } case DeclarationKind::SelfDeclaration: { - FATAL() << "Unreachable TypeChecker `Self` declaration"; + CARBON_FATAL() << "Unreachable TypeChecker `Self` declaration"; } } return Success(); @@ -2113,30 +2145,34 @@ auto TypeChecker::DeclareDeclaration(Nonnull d, switch (d->kind()) { case DeclarationKind::InterfaceDeclaration: { auto& iface_decl = cast(*d); - RETURN_IF_ERROR( + CARBON_RETURN_IF_ERROR( DeclareInterfaceDeclaration(&iface_decl, enclosing_scope)); break; } case DeclarationKind::ImplDeclaration: { auto& impl_decl = cast(*d); - RETURN_IF_ERROR(DeclareImplDeclaration(&impl_decl, enclosing_scope)); + CARBON_RETURN_IF_ERROR( + DeclareImplDeclaration(&impl_decl, enclosing_scope)); break; } case DeclarationKind::FunctionDeclaration: { auto& func_def = cast(*d); - RETURN_IF_ERROR(DeclareFunctionDeclaration(&func_def, enclosing_scope)); + CARBON_RETURN_IF_ERROR( + DeclareFunctionDeclaration(&func_def, enclosing_scope)); break; } case DeclarationKind::ClassDeclaration: { auto& class_decl = cast(*d); - RETURN_IF_ERROR(DeclareClassDeclaration(&class_decl, enclosing_scope)); + CARBON_RETURN_IF_ERROR( + DeclareClassDeclaration(&class_decl, enclosing_scope)); break; } case DeclarationKind::ChoiceDeclaration: { auto& choice = cast(*d); - RETURN_IF_ERROR(DeclareChoiceDeclaration(&choice, enclosing_scope)); + CARBON_RETURN_IF_ERROR( + DeclareChoiceDeclaration(&choice, enclosing_scope)); break; } @@ -2150,16 +2186,16 @@ auto TypeChecker::DeclareDeclaration(Nonnull d, } Expression& type = cast(var.binding().type()).expression(); - RETURN_IF_ERROR(TypeCheckPattern(&var.binding(), std::nullopt, - enclosing_scope, var.value_category())); - ASSIGN_OR_RETURN(Nonnull declared_type, - InterpExp(&type, arena_, trace_stream_)); + CARBON_RETURN_IF_ERROR(TypeCheckPattern( + &var.binding(), std::nullopt, enclosing_scope, var.value_category())); + CARBON_ASSIGN_OR_RETURN(Nonnull declared_type, + InterpExp(&type, arena_, trace_stream_)); var.set_static_type(declared_type); break; } case DeclarationKind::SelfDeclaration: { - FATAL() << "Unreachable TypeChecker declare `Self` declaration"; + CARBON_FATAL() << "Unreachable TypeChecker declare `Self` declaration"; } } return Success(); @@ -2169,9 +2205,9 @@ template void TypeChecker::SetConstantValue(Nonnull value_node, Nonnull value) { std::optional> old_value = value_node->constant_value(); - CHECK(!old_value.has_value()); + CARBON_CHECK(!old_value.has_value()); value_node->set_constant_value(value); - CHECK(constants_.insert(value_node).second); + CARBON_CHECK(constants_.insert(value_node).second); } void TypeChecker::PrintConstants(llvm::raw_ostream& out) { diff --git a/explorer/interpreter/value.cpp b/explorer/interpreter/value.cpp index 6f201998b38b..3410662e844c 100644 --- a/explorer/interpreter/value.cpp +++ b/explorer/interpreter/value.cpp @@ -58,7 +58,7 @@ static auto GetMember(Nonnull arena, Nonnull v, } } default: - FATAL() << "expected Witness, not " << *witness; + CARBON_FATAL() << "expected Witness, not " << *witness; } } switch (v->kind()) { @@ -121,7 +121,7 @@ static auto GetMember(Nonnull arena, Nonnull v, class_type.witnesses()); } default: - FATAL() << "field access not allowed for value " << *v; + CARBON_FATAL() << "field access not allowed for value " << *v; } } @@ -130,7 +130,7 @@ auto Value::GetField(Nonnull arena, const FieldPath& path, -> ErrorOr> { Nonnull value(this); for (const FieldPath::Component& field : path.components_) { - ASSIGN_OR_RETURN(value, GetMember(arena, value, field, source_loc)); + CARBON_ASSIGN_OR_RETURN(value, GetMember(arena, value, field, source_loc)); } return value; } @@ -155,9 +155,9 @@ static auto SetFieldImpl( return RuntimeError(source_loc) << "field " << (*path_begin).name() << " not in " << *value; } - ASSIGN_OR_RETURN(it->value, - SetFieldImpl(arena, it->value, path_begin + 1, path_end, - field_value, source_loc)); + CARBON_ASSIGN_OR_RETURN( + it->value, SetFieldImpl(arena, it->value, path_begin + 1, path_end, + field_value, source_loc)); return arena->New(elements); } case Value::Kind::NominalClassValue: { @@ -173,13 +173,13 @@ static auto SetFieldImpl( return RuntimeError(source_loc) << "index " << (*path_begin).name() << " out of range in " << *value; } - ASSIGN_OR_RETURN(elements[index], - SetFieldImpl(arena, elements[index], path_begin + 1, - path_end, field_value, source_loc)); + CARBON_ASSIGN_OR_RETURN( + elements[index], SetFieldImpl(arena, elements[index], path_begin + 1, + path_end, field_value, source_loc)); return arena->New(elements); } default: - FATAL() << "field access not allowed for value " << *value; + CARBON_FATAL() << "field access not allowed for value " << *value; } } @@ -400,13 +400,13 @@ void Value::Print(llvm::raw_ostream& out) const { } ContinuationValue::StackFragment::~StackFragment() { - CHECK(reversed_todo_.empty()) + CARBON_CHECK(reversed_todo_.empty()) << "All StackFragments must be empty before the Carbon program ends."; } void ContinuationValue::StackFragment::StoreReversed( std::vector> reversed_todo) { - CHECK(reversed_todo_.empty()); + CARBON_CHECK(reversed_todo_.empty()); reversed_todo_ = std::move(reversed_todo); } @@ -545,14 +545,14 @@ auto TypeEqual(Nonnull t1, Nonnull t2) -> bool { case Value::Kind::BindingPlaceholderValue: case Value::Kind::ContinuationValue: case Value::Kind::ParameterizedEntityName: - FATAL() << "TypeEqual used to compare non-type values\n" - << *t1 << "\n" - << *t2; + CARBON_FATAL() << "TypeEqual used to compare non-type values\n" + << *t1 << "\n" + << *t2; case Value::Kind::Witness: - FATAL() << "TypeEqual: unexpected Witness"; + CARBON_FATAL() << "TypeEqual: unexpected Witness"; break; case Value::Kind::AutoType: - FATAL() << "TypeEqual: unexpected AutoType"; + CARBON_FATAL() << "TypeEqual: unexpected AutoType"; break; } } @@ -604,9 +604,10 @@ auto ValueEqual(Nonnull v1, Nonnull v2) -> bool { case Value::Kind::StructValue: { const auto& struct_v1 = cast(*v1); const auto& struct_v2 = cast(*v2); - CHECK(struct_v1.elements().size() == struct_v2.elements().size()); + CARBON_CHECK(struct_v1.elements().size() == struct_v2.elements().size()); for (size_t i = 0; i < struct_v1.elements().size(); ++i) { - CHECK(struct_v1.elements()[i].name == struct_v2.elements()[i].name); + CARBON_CHECK(struct_v1.elements()[i].name == + struct_v2.elements()[i].name); if (!ValueEqual(struct_v1.elements()[i].value, struct_v2.elements()[i].value)) { return false; @@ -621,7 +622,7 @@ auto ValueEqual(Nonnull v1, Nonnull v2) -> bool { GetName(cast(v1)->declaration()); std::optional name2 = GetName(cast(v2)->declaration()); - CHECK(name1.has_value() && name2.has_value()) + CARBON_CHECK(name1.has_value() && name2.has_value()) << "parameterized name refers to unnamed declaration"; return *name1 == *name2; } @@ -654,7 +655,8 @@ auto ValueEqual(Nonnull v1, Nonnull v2) -> bool { case Value::Kind::LValue: // TODO: support pointer comparisons once we have a clearer distinction // between pointers and lvalues. - FATAL() << "ValueEqual does not support this kind of value: " << *v1; + CARBON_FATAL() << "ValueEqual does not support this kind of value: " + << *v1; } } diff --git a/explorer/interpreter/value.h b/explorer/interpreter/value.h index 6cff29043a67..88396004d4e5 100644 --- a/explorer/interpreter/value.h +++ b/explorer/interpreter/value.h @@ -263,7 +263,7 @@ class StructValue : public Value { public: explicit StructValue(std::vector elements) : Value(Kind::StructValue), elements_(std::move(elements)) { - CHECK(!elements_.empty()) + CARBON_CHECK(!elements_.empty()) << "`{}` is represented as a StructType, not a StructValue."; } @@ -511,7 +511,7 @@ class NominalClassType : public Value { // Construct a non-generic class type. explicit NominalClassType(Nonnull declaration) : Value(Kind::NominalClassType), declaration_(declaration) { - CHECK(!declaration->type_params().has_value()) + CARBON_CHECK(!declaration->type_params().has_value()) << "missing arguments for parameterized class type"; } @@ -592,7 +592,7 @@ class InterfaceType : public Value { public: explicit InterfaceType(Nonnull declaration) : Value(Kind::InterfaceType), declaration_(declaration) { - CHECK(!declaration->params().has_value()) + CARBON_CHECK(!declaration->params().has_value()) << "missing arguments for parameterized interface type"; } explicit InterfaceType(Nonnull declaration, diff --git a/explorer/main.cpp b/explorer/main.cpp index 589fbef31d3d..f159a14e617f 100644 --- a/explorer/main.cpp +++ b/explorer/main.cpp @@ -70,11 +70,13 @@ static auto Main(llvm::StringRef default_prelude_file, int argc, char* argv[]) } Arena arena; - ASSIGN_OR_RETURN(AST ast, Parse(&arena, input_file_name, parser_debug)); + CARBON_ASSIGN_OR_RETURN(AST ast, + Parse(&arena, input_file_name, parser_debug)); AddPrelude(prelude_file_name, &arena, &ast.declarations); // Typecheck and run the parsed program. - ASSIGN_OR_RETURN(int return_code, ExecProgram(&arena, ast, trace_stream)); + CARBON_ASSIGN_OR_RETURN(int return_code, + ExecProgram(&arena, ast, trace_stream)); // Print the return code to stdout even when we aren't tracing. (trace_stream ? **trace_stream : llvm::outs()) << "result: " << return_code << "\n"; diff --git a/explorer/syntax/bison_wrap.h b/explorer/syntax/bison_wrap.h index 743ef8560bde..ebe8040d28b0 100644 --- a/explorer/syntax/bison_wrap.h +++ b/explorer/syntax/bison_wrap.h @@ -30,7 +30,7 @@ class BisonWrap { // Deliberately releases the contained value. Errors if not initialized. // Called directly in parser.ypp when releasing pairs. auto Release() -> T { - CHECK(val_.has_value()); + CARBON_CHECK(val_.has_value()); T ret = std::move(*val_); val_.reset(); return ret; diff --git a/explorer/syntax/lexer.lpp b/explorer/syntax/lexer.lpp index e261f17a5a10..12db8d242804 100644 --- a/explorer/syntax/lexer.lpp +++ b/explorer/syntax/lexer.lpp @@ -283,7 +283,7 @@ string_literal \"([^\\\"\n\v\f\r]|\\.)*\" {string_literal} { llvm::StringRef str(yytext); - CHECK(str.consume_front("\"") && str.consume_back("\"")); + CARBON_CHECK(str.consume_front("\"") && str.consume_back("\"")); std::optional unescaped = Carbon::UnescapeStringLiteral(str); if (unescaped == std::nullopt) { return context.RecordSyntaxError( diff --git a/explorer/syntax/parse.cpp b/explorer/syntax/parse.cpp index 492c933219b2..ef065afa19f9 100644 --- a/explorer/syntax/parse.cpp +++ b/explorer/syntax/parse.cpp @@ -36,7 +36,7 @@ static auto ParseImpl(yyscan_t scanner, Nonnull arena, } // Return parse results. - CHECK(ast != std::nullopt) + CARBON_CHECK(ast != std::nullopt) << "parser validated syntax yet didn't produce an AST."; return *ast; } diff --git a/explorer/syntax/parser.ypp b/explorer/syntax/parser.ypp index c222528376fc..8095c0c05a3f 100644 --- a/explorer/syntax/parser.ypp +++ b/explorer/syntax/parser.ypp @@ -300,8 +300,8 @@ primary_expression: | sized_type_literal { int val; - CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val)); - CHECK($1[0] == 'i' && val == 32) + CARBON_CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val)); + CARBON_CHECK($1[0] == 'i' && val == 32) << "Only i32 is supported for now: " << $1; $$ = arena->New(context.source_loc()); } diff --git a/explorer/syntax/prelude.cpp b/explorer/syntax/prelude.cpp index 1df912c739e9..c0c943079b8e 100644 --- a/explorer/syntax/prelude.cpp +++ b/explorer/syntax/prelude.cpp @@ -15,8 +15,8 @@ void AddPrelude(std::string_view prelude_file_name, Nonnull arena, if (!parse_result.ok()) { // Try again with tracing, to help diagnose the problem. ErrorOr trace_parse_result = Parse(arena, prelude_file_name, true); - FATAL() << "Failed to parse prelude: " - << trace_parse_result.error().message(); + CARBON_FATAL() << "Failed to parse prelude: " + << trace_parse_result.error().message(); } const auto& prelude = *parse_result; declarations->insert(declarations->begin(), prelude.declarations.begin(), diff --git a/toolchain/diagnostics/diagnostic_kind.cpp b/toolchain/diagnostics/diagnostic_kind.cpp index 9eadb828f752..c14df46eac0d 100644 --- a/toolchain/diagnostics/diagnostic_kind.cpp +++ b/toolchain/diagnostics/diagnostic_kind.cpp @@ -9,7 +9,7 @@ namespace Carbon { auto operator<<(llvm::raw_ostream& out, DiagnosticKind kind) -> llvm::raw_ostream& { static constexpr llvm::StringLiteral Names[] = { -#define DIAGNOSTIC_KIND(DiagnosticName) #DiagnosticName, +#define CARBON_DIAGNOSTIC_KIND(DiagnosticName) #DiagnosticName, #include "toolchain/diagnostics/diagnostic_registry.def" }; out << Names[static_cast(kind)]; diff --git a/toolchain/diagnostics/diagnostic_kind.h b/toolchain/diagnostics/diagnostic_kind.h index f767fb398813..73d2c53703e8 100644 --- a/toolchain/diagnostics/diagnostic_kind.h +++ b/toolchain/diagnostics/diagnostic_kind.h @@ -19,7 +19,7 @@ namespace Carbon { // long-term, and we also see value to keeping diagnostic format strings close // to the consuming code. enum class DiagnosticKind : int32_t { -#define DIAGNOSTIC_KIND(DiagnosticName) DiagnosticName, +#define CARBON_DIAGNOSTIC_KIND(DiagnosticName) DiagnosticName, #include "toolchain/diagnostics/diagnostic_registry.def" }; diff --git a/toolchain/diagnostics/diagnostic_registry.def b/toolchain/diagnostics/diagnostic_registry.def index 9d3fa586e8ea..1862af2efa30 100644 --- a/toolchain/diagnostics/diagnostic_registry.def +++ b/toolchain/diagnostics/diagnostic_registry.def @@ -16,61 +16,61 @@ // Lexer diagnostics // ============================================================================ -DIAGNOSTIC_KIND(BinaryRealLiteral) -DIAGNOSTIC_KIND(ContentBeforeStringTerminator) -DIAGNOSTIC_KIND(DecimalEscapeSequence) -DIAGNOSTIC_KIND(EmptyDigitSequence) -DIAGNOSTIC_KIND(HexadecimalEscapeMissingDigits) -DIAGNOSTIC_KIND(InvalidDigit) -DIAGNOSTIC_KIND(InvalidDigitSeparator) -DIAGNOSTIC_KIND(InvalidHorizontalWhitespaceInString) -DIAGNOSTIC_KIND(IrregularDigitSeparators) -DIAGNOSTIC_KIND(MismatchedClosing) -DIAGNOSTIC_KIND(MismatchedIndentInString) -DIAGNOSTIC_KIND(NoWhitespaceAfterCommentIntroducer) -DIAGNOSTIC_KIND(TooManyDigits) -DIAGNOSTIC_KIND(TrailingComment) -DIAGNOSTIC_KIND(UnicodeEscapeMissingBracedDigits) -DIAGNOSTIC_KIND(UnicodeEscapeSurrogate) -DIAGNOSTIC_KIND(UnicodeEscapeTooLarge) -DIAGNOSTIC_KIND(UnknownBaseSpecifier) -DIAGNOSTIC_KIND(UnknownEscapeSequence) -DIAGNOSTIC_KIND(UnmatchedClosing) -DIAGNOSTIC_KIND(UnrecognizedCharacters) -DIAGNOSTIC_KIND(UnterminatedString) -DIAGNOSTIC_KIND(WrongRealLiteralExponent) +CARBON_DIAGNOSTIC_KIND(BinaryRealLiteral) +CARBON_DIAGNOSTIC_KIND(ContentBeforeStringTerminator) +CARBON_DIAGNOSTIC_KIND(DecimalEscapeSequence) +CARBON_DIAGNOSTIC_KIND(EmptyDigitSequence) +CARBON_DIAGNOSTIC_KIND(HexadecimalEscapeMissingDigits) +CARBON_DIAGNOSTIC_KIND(InvalidDigit) +CARBON_DIAGNOSTIC_KIND(InvalidDigitSeparator) +CARBON_DIAGNOSTIC_KIND(InvalidHorizontalWhitespaceInString) +CARBON_DIAGNOSTIC_KIND(IrregularDigitSeparators) +CARBON_DIAGNOSTIC_KIND(MismatchedClosing) +CARBON_DIAGNOSTIC_KIND(MismatchedIndentInString) +CARBON_DIAGNOSTIC_KIND(NoWhitespaceAfterCommentIntroducer) +CARBON_DIAGNOSTIC_KIND(TooManyDigits) +CARBON_DIAGNOSTIC_KIND(TrailingComment) +CARBON_DIAGNOSTIC_KIND(UnicodeEscapeMissingBracedDigits) +CARBON_DIAGNOSTIC_KIND(UnicodeEscapeSurrogate) +CARBON_DIAGNOSTIC_KIND(UnicodeEscapeTooLarge) +CARBON_DIAGNOSTIC_KIND(UnknownBaseSpecifier) +CARBON_DIAGNOSTIC_KIND(UnknownEscapeSequence) +CARBON_DIAGNOSTIC_KIND(UnmatchedClosing) +CARBON_DIAGNOSTIC_KIND(UnrecognizedCharacters) +CARBON_DIAGNOSTIC_KIND(UnterminatedString) +CARBON_DIAGNOSTIC_KIND(WrongRealLiteralExponent) // ============================================================================ // Parser diagnostics // ============================================================================ -DIAGNOSTIC_KIND(BinaryOperatorRequiresWhitespace) -DIAGNOSTIC_KIND(ExpectedCloseParen) -DIAGNOSTIC_KIND(ExpectedCodeBlock) -DIAGNOSTIC_KIND(ExpectedExpression) -DIAGNOSTIC_KIND(ExpectedFunctionBodyOrSemi) -DIAGNOSTIC_KIND(ExpectedFunctionName) -DIAGNOSTIC_KIND(ExpectedFunctionParams) -DIAGNOSTIC_KIND(ExpectedIdentifierAfterDot) -DIAGNOSTIC_KIND(ExpectedParameterName) -DIAGNOSTIC_KIND(ExpectedParenAfter) -DIAGNOSTIC_KIND(ExpectedSemiAfter) -DIAGNOSTIC_KIND(ExpectedSemiAfterExpression) -DIAGNOSTIC_KIND(ExpectedStructLiteralField) -DIAGNOSTIC_KIND(ExpectedVariableName) -DIAGNOSTIC_KIND(OperatorRequiresParentheses) -DIAGNOSTIC_KIND(StackLimitExceeded) -DIAGNOSTIC_KIND(UnaryOperatorHasWhitespace) -DIAGNOSTIC_KIND(UnaryOperatorRequiresWhitespace) -DIAGNOSTIC_KIND(UnexpectedTokenAfterListElement) -DIAGNOSTIC_KIND(UnexpectedTokenInCodeBlock) -DIAGNOSTIC_KIND(UnrecognizedDeclaration) +CARBON_DIAGNOSTIC_KIND(BinaryOperatorRequiresWhitespace) +CARBON_DIAGNOSTIC_KIND(ExpectedCloseParen) +CARBON_DIAGNOSTIC_KIND(ExpectedCodeBlock) +CARBON_DIAGNOSTIC_KIND(ExpectedExpression) +CARBON_DIAGNOSTIC_KIND(ExpectedFunctionBodyOrSemi) +CARBON_DIAGNOSTIC_KIND(ExpectedFunctionName) +CARBON_DIAGNOSTIC_KIND(ExpectedFunctionParams) +CARBON_DIAGNOSTIC_KIND(ExpectedIdentifierAfterDot) +CARBON_DIAGNOSTIC_KIND(ExpectedParameterName) +CARBON_DIAGNOSTIC_KIND(ExpectedParenAfter) +CARBON_DIAGNOSTIC_KIND(ExpectedSemiAfter) +CARBON_DIAGNOSTIC_KIND(ExpectedSemiAfterExpression) +CARBON_DIAGNOSTIC_KIND(ExpectedStructLiteralField) +CARBON_DIAGNOSTIC_KIND(ExpectedVariableName) +CARBON_DIAGNOSTIC_KIND(OperatorRequiresParentheses) +CARBON_DIAGNOSTIC_KIND(StackLimitExceeded) +CARBON_DIAGNOSTIC_KIND(UnaryOperatorHasWhitespace) +CARBON_DIAGNOSTIC_KIND(UnaryOperatorRequiresWhitespace) +CARBON_DIAGNOSTIC_KIND(UnexpectedTokenAfterListElement) +CARBON_DIAGNOSTIC_KIND(UnexpectedTokenInCodeBlock) +CARBON_DIAGNOSTIC_KIND(UnrecognizedDeclaration) // ============================================================================ // Other diagnostics // ============================================================================ // TestDiagnostic is only for unit tests. -DIAGNOSTIC_KIND(TestDiagnostic) +CARBON_DIAGNOSTIC_KIND(TestDiagnostic) -#undef DIAGNOSTIC_KIND +#undef CARBON_DIAGNOSTIC_KIND diff --git a/toolchain/lexer/numeric_literal.cpp b/toolchain/lexer/numeric_literal.cpp index 855ef28efd9a..06c153cfdcf4 100644 --- a/toolchain/lexer/numeric_literal.cpp +++ b/toolchain/lexer/numeric_literal.cpp @@ -78,7 +78,7 @@ auto LexedNumericLiteral::Lex(llvm::StringRef source_text) IsAlnum(source_text[i + 1])) { // This is not possible because we don't update result.exponent after we // see a '+' or '-'. - CHECK(!seen_plus_minus) << "should only consume one + or -"; + CARBON_CHECK(!seen_plus_minus) << "should only consume one + or -"; seen_plus_minus = true; continue; } @@ -336,7 +336,8 @@ auto LexedNumericLiteral::Parser::CheckDigitSequence( // correctly positioned. auto LexedNumericLiteral::Parser::CheckDigitSeparatorPlacement( llvm::StringRef text, Radix radix, int num_digit_separators) -> void { - DCHECK(std::count(text.begin(), text.end(), '_') == num_digit_separators) + CARBON_DCHECK(std::count(text.begin(), text.end(), '_') == + num_digit_separators) << "given wrong number of digit separators: " << num_digit_separators; if (radix == Radix::Binary) { diff --git a/toolchain/lexer/numeric_literal_benchmark.cpp b/toolchain/lexer/numeric_literal_benchmark.cpp index c0d3b6ea6947..04bd3e974523 100644 --- a/toolchain/lexer/numeric_literal_benchmark.cpp +++ b/toolchain/lexer/numeric_literal_benchmark.cpp @@ -13,19 +13,19 @@ namespace { static void BM_Lex_Float(benchmark::State& state) { for (auto _ : state) { - CHECK(LexedNumericLiteral::Lex("0.000001")); + CARBON_CHECK(LexedNumericLiteral::Lex("0.000001")); } } static void BM_Lex_Integer(benchmark::State& state) { for (auto _ : state) { - CHECK(LexedNumericLiteral::Lex("1_234_567_890")); + CARBON_CHECK(LexedNumericLiteral::Lex("1_234_567_890")); } } static void BM_ComputeValue_Float(benchmark::State& state) { auto val = LexedNumericLiteral::Lex("0.000001"); - CHECK(val); + CARBON_CHECK(val); auto emitter = NullDiagnosticEmitter(); for (auto _ : state) { val->ComputeValue(emitter); @@ -35,7 +35,7 @@ static void BM_ComputeValue_Float(benchmark::State& state) { static void BM_ComputeValue_Integer(benchmark::State& state) { auto val = LexedNumericLiteral::Lex("1_234_567_890"); auto emitter = NullDiagnosticEmitter(); - CHECK(val); + CARBON_CHECK(val); for (auto _ : state) { val->ComputeValue(emitter); } diff --git a/toolchain/lexer/numeric_literal_test.cpp b/toolchain/lexer/numeric_literal_test.cpp index e5f51abb15a4..45273b3ffe6d 100644 --- a/toolchain/lexer/numeric_literal_test.cpp +++ b/toolchain/lexer/numeric_literal_test.cpp @@ -32,7 +32,7 @@ class NumericLiteralTest : public ::testing::Test { auto Lex(llvm::StringRef text) -> LexedNumericLiteral { llvm::Optional result = LexedNumericLiteral::Lex(text); - CHECK(result); + CARBON_CHECK(result); EXPECT_EQ(result->text(), text); return *result; } diff --git a/toolchain/lexer/string_literal.cpp b/toolchain/lexer/string_literal.cpp index 07df9b06f285..2528bf73e91b 100644 --- a/toolchain/lexer/string_literal.cpp +++ b/toolchain/lexer/string_literal.cpp @@ -208,7 +208,7 @@ static auto ExpandUnicodeEscapeSequence(LexerDiagnosticEmitter& emitter, static auto ExpandAndConsumeEscapeSequence(LexerDiagnosticEmitter& emitter, llvm::StringRef& content, std::string& result) -> void { - CHECK(!content.empty()) << "should have escaped closing delimiter"; + CARBON_CHECK(!content.empty()) << "should have escaped closing delimiter"; char first = content.front(); content = content.drop_front(1); @@ -343,7 +343,7 @@ static auto ExpandEscapeSequencesAndRemoveIndent( if (IsHorizontalWhitespace(contents.front())) { // Horizontal whitespace other than ` ` is valid only at the end of a // line. - CHECK(contents.front() != ' ') + CARBON_CHECK(contents.front() != ' ') << "should not have stopped at a plain space"; auto after_space = contents.find_if_not(IsHorizontalWhitespace); if (after_space == llvm::StringRef::npos || diff --git a/toolchain/lexer/string_literal_fuzzer.cpp b/toolchain/lexer/string_literal_fuzzer.cpp index 02cf4e7e9ded..b3259ae66f63 100644 --- a/toolchain/lexer/string_literal_fuzzer.cpp +++ b/toolchain/lexer/string_literal_fuzzer.cpp @@ -33,7 +33,7 @@ extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, fprintf(stderr, "text: %s\n", token->text().str().c_str()); // Check multiline flag was computed correctly. - CHECK(token->is_multi_line() == token->text().contains('\n')); + CARBON_CHECK(token->is_multi_line() == token->text().contains('\n')); volatile auto value = token->ComputeValue(NullDiagnosticEmitter()); diff --git a/toolchain/lexer/string_literal_test.cpp b/toolchain/lexer/string_literal_test.cpp index b38e7155b27d..ac0420c36f9d 100644 --- a/toolchain/lexer/string_literal_test.cpp +++ b/toolchain/lexer/string_literal_test.cpp @@ -21,7 +21,7 @@ class StringLiteralTest : public ::testing::Test { auto Lex(llvm::StringRef text) -> LexedStringLiteral { llvm::Optional result = LexedStringLiteral::Lex(text); - CHECK(result); + CARBON_CHECK(result); EXPECT_EQ(result->text(), text); return *result; } diff --git a/toolchain/lexer/test_helpers.h b/toolchain/lexer/test_helpers.h index 5258ede68156..5eb6bcbcc177 100644 --- a/toolchain/lexer/test_helpers.h +++ b/toolchain/lexer/test_helpers.h @@ -29,7 +29,7 @@ class SingleTokenDiagnosticTranslator : token_(token) {} auto GetLocation(const char* pos) -> DiagnosticLocation override { - CHECK(StringRefContainsPointer(token_, pos)) + CARBON_CHECK(StringRefContainsPointer(token_, pos)) << "invalid diagnostic location"; llvm::StringRef prefix = token_.take_front(pos - token_.begin()); auto [before_last_newline, this_line] = prefix.rsplit('\n'); diff --git a/toolchain/lexer/token_kind.cpp b/toolchain/lexer/token_kind.cpp index ff1ff2dd356d..c1804fc125a4 100644 --- a/toolchain/lexer/token_kind.cpp +++ b/toolchain/lexer/token_kind.cpp @@ -56,7 +56,7 @@ auto TokenKind::GetClosingSymbol() const -> TokenKind { #include "toolchain/lexer/token_registry.def" }; auto result = Table[static_cast(kind_value_)]; - CHECK(result != Error()) << "Only opening symbols are valid!"; + CARBON_CHECK(result != Error()) << "Only opening symbols are valid!"; return result; } @@ -78,7 +78,7 @@ auto TokenKind::GetOpeningSymbol() const -> TokenKind { #include "toolchain/lexer/token_registry.def" }; auto result = Table[static_cast(kind_value_)]; - CHECK(result != Error()) << "Only closing symbols are valid!"; + CARBON_CHECK(result != Error()) << "Only closing symbols are valid!"; return result; } diff --git a/toolchain/lexer/tokenized_buffer.cpp b/toolchain/lexer/tokenized_buffer.cpp index 2fc98802546c..d2a6c4e961ad 100644 --- a/toolchain/lexer/tokenized_buffer.cpp +++ b/toolchain/lexer/tokenized_buffer.cpp @@ -144,7 +144,7 @@ class TokenizedBuffer::Lexer { default: // If we find a non-whitespace character without exhausting the // buffer, return true to continue lexing. - CHECK(!IsSpace(source_text.front())); + CARBON_CHECK(!IsSpace(source_text.front())); if (whitespace_start != source_text.begin()) { NoteWhitespace(); } @@ -176,7 +176,8 @@ class TokenizedBuffer::Lexer { } } - CHECK(source_text.empty()) << "Cannot reach here w/o finishing the text!"; + CARBON_CHECK(source_text.empty()) + << "Cannot reach here w/o finishing the text!"; // Update the line length as this is also the end of a line. current_line_info_->length = current_column_; return false; @@ -218,8 +219,8 @@ class TokenizedBuffer::Lexer { buffer_.literal_int_storage_.size(); buffer_.literal_int_storage_.push_back(std::move(value.mantissa)); buffer_.literal_int_storage_.push_back(std::move(value.exponent)); - CHECK(buffer_.GetRealLiteral(token).IsDecimal() == - (value.radix == LexedNumericLiteral::Radix::Decimal)); + CARBON_CHECK(buffer_.GetRealLiteral(token).IsDecimal() == + (value.radix == LexedNumericLiteral::Radix::Decimal)); return token; }, [&](LexedNumericLiteral::UnrecoverableError) { @@ -415,7 +416,8 @@ class TokenizedBuffer::Lexer { "Closing symbol does not match most recent opening symbol."); token_emitter_.Emit(opening_token, MismatchedClosing); - CHECK(!buffer_.tokens().empty()) << "Must have a prior opening token!"; + CARBON_CHECK(!buffer_.tokens().empty()) + << "Must have a prior opening token!"; Token prev_token = buffer_.tokens().end()[-1]; // TODO: do a smarter backwards scan for where to put the closing @@ -455,7 +457,8 @@ class TokenizedBuffer::Lexer { // Take the valid characters off the front of the source buffer. llvm::StringRef identifier_text = source_text.take_while([](char c) { return IsAlnum(c) || c == '_'; }); - CHECK(!identifier_text.empty()) << "Must have at least one character!"; + CARBON_CHECK(!identifier_text.empty()) + << "Must have at least one character!"; int identifier_column = current_column_; current_column_ += identifier_text.size(); source_text = source_text.drop_front(identifier_text.size()); @@ -567,7 +570,7 @@ auto TokenizedBuffer::Lex(SourceBuffer& source, DiagnosticConsumer& consumer) if (!result) { result = lexer.LexError(source_text); } - CHECK(result) << "No token was lexed."; + CARBON_CHECK(result) << "No token was lexed."; } // The end-of-file token is always considered to be whitespace. @@ -620,7 +623,7 @@ auto TokenizedBuffer::GetTokenText(Token token) const -> llvm::StringRef { int64_t token_start = line_info.start + token_info.column; llvm::Optional relexed_token = LexedNumericLiteral::Lex(source_->text().substr(token_start)); - CHECK(relexed_token) << "Could not reform numeric literal token."; + CARBON_CHECK(relexed_token) << "Could not reform numeric literal token."; return relexed_token->text(); } @@ -631,7 +634,7 @@ auto TokenizedBuffer::GetTokenText(Token token) const -> llvm::StringRef { int64_t token_start = line_info.start + token_info.column; llvm::Optional relexed_token = LexedStringLiteral::Lex(source_->text().substr(token_start)); - CHECK(relexed_token) << "Could not reform string literal token."; + CARBON_CHECK(relexed_token) << "Could not reform string literal token."; return relexed_token->text(); } @@ -649,14 +652,14 @@ auto TokenizedBuffer::GetTokenText(Token token) const -> llvm::StringRef { return llvm::StringRef(); } - CHECK(token_info.kind == TokenKind::Identifier()) + CARBON_CHECK(token_info.kind == TokenKind::Identifier()) << "Only identifiers have stored text!"; return GetIdentifierText(token_info.id); } auto TokenizedBuffer::GetIdentifier(Token token) const -> Identifier { auto& token_info = GetTokenInfo(token); - CHECK(token_info.kind == TokenKind::Identifier()) + CARBON_CHECK(token_info.kind == TokenKind::Identifier()) << "The token must be an identifier!"; return token_info.id; } @@ -664,14 +667,14 @@ auto TokenizedBuffer::GetIdentifier(Token token) const -> Identifier { auto TokenizedBuffer::GetIntegerLiteral(Token token) const -> const llvm::APInt& { auto& token_info = GetTokenInfo(token); - CHECK(token_info.kind == TokenKind::IntegerLiteral()) + CARBON_CHECK(token_info.kind == TokenKind::IntegerLiteral()) << "The token must be an integer literal!"; return literal_int_storage_[token_info.literal_index]; } auto TokenizedBuffer::GetRealLiteral(Token token) const -> RealLiteralValue { auto& token_info = GetTokenInfo(token); - CHECK(token_info.kind == TokenKind::RealLiteral()) + CARBON_CHECK(token_info.kind == TokenKind::RealLiteral()) << "The token must be a real literal!"; // Note that every real literal is at least three characters long, so we can @@ -687,7 +690,7 @@ auto TokenizedBuffer::GetRealLiteral(Token token) const -> RealLiteralValue { auto TokenizedBuffer::GetStringLiteral(Token token) const -> llvm::StringRef { auto& token_info = GetTokenInfo(token); - CHECK(token_info.kind == TokenKind::StringLiteral()) + CARBON_CHECK(token_info.kind == TokenKind::StringLiteral()) << "The token must be a string literal!"; return literal_string_storage_[token_info.literal_index]; } @@ -695,7 +698,7 @@ auto TokenizedBuffer::GetStringLiteral(Token token) const -> llvm::StringRef { auto TokenizedBuffer::GetTypeLiteralSize(Token token) const -> const llvm::APInt& { auto& token_info = GetTokenInfo(token); - CHECK(token_info.kind.IsSizedTypeLiteral()) + CARBON_CHECK(token_info.kind.IsSizedTypeLiteral()) << "The token must be a sized type literal!"; return literal_int_storage_[token_info.literal_index]; } @@ -703,7 +706,7 @@ auto TokenizedBuffer::GetTypeLiteralSize(Token token) const auto TokenizedBuffer::GetMatchedClosingToken(Token opening_token) const -> Token { auto& opening_token_info = GetTokenInfo(opening_token); - CHECK(opening_token_info.kind.IsOpeningSymbol()) + CARBON_CHECK(opening_token_info.kind.IsOpeningSymbol()) << "The token must be an opening group symbol!"; return opening_token_info.closing_token; } @@ -711,7 +714,7 @@ auto TokenizedBuffer::GetMatchedClosingToken(Token opening_token) const auto TokenizedBuffer::GetMatchedOpeningToken(Token closing_token) const -> Token { auto& closing_token_info = GetTokenInfo(closing_token); - CHECK(closing_token_info.kind.IsClosingSymbol()) + CARBON_CHECK(closing_token_info.kind.IsClosingSymbol()) << "The token must be an closing group symbol!"; return closing_token_info.opening_token; } @@ -756,7 +759,7 @@ auto TokenizedBuffer::PrintWidths::Widen(const PrintWidths& widths) -> void { // // This routine requires its argument to be *non-negative*. static auto ComputeDecimalPrintedWidth(int number) -> int { - CHECK(number >= 0) << "Negative numbers are not supported."; + CARBON_CHECK(number >= 0) << "Negative numbers are not supported."; if (number == 0) { return 1; } @@ -887,7 +890,7 @@ auto TokenizedBuffer::TokenIterator::Print(llvm::raw_ostream& output) const auto TokenizedBuffer::SourceBufferLocationTranslator::GetLocation( const char* loc) -> DiagnosticLocation { - CHECK(StringRefContainsPointer(buffer_->source_->text(), loc)) + CARBON_CHECK(StringRefContainsPointer(buffer_->source_->text(), loc)) << "location not within buffer"; int64_t offset = loc - buffer_->source_->text().begin(); @@ -901,7 +904,7 @@ auto TokenizedBuffer::SourceBufferLocationTranslator::GetLocation( line_it == buffer_->line_infos_.end(); // Step back one line to find the line containing the given position. - CHECK(line_it != buffer_->line_infos_.begin()) + CARBON_CHECK(line_it != buffer_->line_infos_.begin()) << "location precedes the start of the first line"; --line_it; int line_number = line_it - buffer_->line_infos_.begin(); diff --git a/toolchain/lexer/tokenized_buffer_fuzzer.cpp b/toolchain/lexer/tokenized_buffer_fuzzer.cpp index 10524200b952..5c88e8c5f30b 100644 --- a/toolchain/lexer/tokenized_buffer_fuzzer.cpp +++ b/toolchain/lexer/tokenized_buffer_fuzzer.cpp @@ -39,11 +39,11 @@ extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, // token stream. for (TokenizedBuffer::Token token : buffer.tokens()) { int line_number = buffer.GetLineNumber(token); - CHECK(line_number > 0) << "Invalid line number!"; - CHECK(line_number < INT_MAX) << "Invalid line number!"; + CARBON_CHECK(line_number > 0) << "Invalid line number!"; + CARBON_CHECK(line_number < INT_MAX) << "Invalid line number!"; int column_number = buffer.GetColumnNumber(token); - CHECK(column_number > 0) << "Invalid line number!"; - CHECK(column_number < INT_MAX) << "Invalid line number!"; + CARBON_CHECK(column_number > 0) << "Invalid line number!"; + CARBON_CHECK(column_number < INT_MAX) << "Invalid line number!"; } return 0; diff --git a/toolchain/lexer/tokenized_buffer_test_helpers.h b/toolchain/lexer/tokenized_buffer_test_helpers.h index cee2cb9e3e8a..10f3c09ab72b 100644 --- a/toolchain/lexer/tokenized_buffer_test_helpers.h +++ b/toolchain/lexer/tokenized_buffer_test_helpers.h @@ -131,8 +131,8 @@ MATCHER_P(HasTokens, raw_all_expected, "") { matches = false; } - CHECK(!expected.string_contents || - expected.kind == TokenKind::StringLiteral()); + CARBON_CHECK(!expected.string_contents || + expected.kind == TokenKind::StringLiteral()); if (expected.string_contents && actual_kind == TokenKind::StringLiteral()) { llvm::StringRef actual_contents = buffer.GetStringLiteral(token); if (actual_contents != *expected.string_contents) { diff --git a/toolchain/parser/parse_test_helpers.h b/toolchain/parser/parse_test_helpers.h index 64259fd3c9f0..c0b98bd6639f 100644 --- a/toolchain/parser/parse_test_helpers.h +++ b/toolchain/parser/parse_test_helpers.h @@ -101,7 +101,7 @@ inline auto ExpectedNodesMatcher::MatchAndExplain( } if (expected_node.skip_subtree) { - CHECK(expected_node.children.empty()) + CARBON_CHECK(expected_node.children.empty()) << "Must not skip an expected subtree while specifying expected " "children!"; nodes_it = llvm::reverse(tree.postorder(n)).end(); @@ -138,7 +138,7 @@ inline auto ExpectedNodesMatcher::MatchAndExplain( // subtrees. Instead, we need to check that we successfully processed all of // the actual tree and consumed all of the expected tree. if (nodes_it != nodes_end) { - CHECK(expected_node_stack.empty()) + CARBON_CHECK(expected_node_stack.empty()) << "If we have unmatched nodes in the input tree, should only finish " "having fully processed expected tree."; output << "\nFinished processing expected nodes and there are still " @@ -193,7 +193,7 @@ inline auto ExpectedNodesMatcher::DescribeTo(std::ostream* output_ptr) const } if (!expected_node.children.empty()) { - CHECK(!expected_node.skip_subtree) + CARBON_CHECK(!expected_node.skip_subtree) << "Must not have children and skip a subtree!"; output << ", children: [\n"; for (const ExpectedNode& child_expected_node : @@ -209,7 +209,7 @@ inline auto ExpectedNodesMatcher::DescribeTo(std::ostream* output_ptr) const // we pop up. output << "}"; if (!expected_node_stack.empty()) { - CHECK(depth >= expected_node_stack.back().second) + CARBON_CHECK(depth >= expected_node_stack.back().second) << "Cannot have an increase in depth on a leaf node!"; // The distance we need to pop is the difference in depth. int pop_depth = depth - expected_node_stack.back().second; diff --git a/toolchain/parser/parse_tree.cpp b/toolchain/parser/parse_tree.cpp index 3a37375175a9..3c98b3fa0dc2 100644 --- a/toolchain/parser/parse_tree.cpp +++ b/toolchain/parser/parse_tree.cpp @@ -37,7 +37,7 @@ auto ParseTree::postorder() const -> llvm::iterator_range { auto ParseTree::postorder(Node n) const -> llvm::iterator_range { - CHECK(n.is_valid()); + CARBON_CHECK(n.is_valid()); // The postorder ends after this node, the root, and begins at the start of // its subtree. int end_index = n.index_ + 1; @@ -48,7 +48,7 @@ auto ParseTree::postorder(Node n) const auto ParseTree::children(Node n) const -> llvm::iterator_range { - CHECK(n.is_valid()); + CARBON_CHECK(n.is_valid()); int end_index = n.index_ - node_impls_[n.index_].subtree_size; return {SiblingIterator(*this, Node(n.index_ - 1)), SiblingIterator(*this, Node(end_index))}; @@ -61,22 +61,22 @@ auto ParseTree::roots() const -> llvm::iterator_range { } auto ParseTree::node_has_error(Node n) const -> bool { - CHECK(n.is_valid()); + CARBON_CHECK(n.is_valid()); return node_impls_[n.index_].has_error; } auto ParseTree::node_kind(Node n) const -> ParseNodeKind { - CHECK(n.is_valid()); + CARBON_CHECK(n.is_valid()); return node_impls_[n.index_].kind; } auto ParseTree::node_token(Node n) const -> TokenizedBuffer::Token { - CHECK(n.is_valid()); + CARBON_CHECK(n.is_valid()); return node_impls_[n.index_].token; } auto ParseTree::GetNodeText(Node n) const -> llvm::StringRef { - CHECK(n.is_valid()); + CARBON_CHECK(n.is_valid()); return tokens_->GetTokenText(node_impls_[n.index_].token); } @@ -126,12 +126,12 @@ auto ParseTree::Print(llvm::raw_ostream& output) const -> void { } // This node is finished, so close it up. - CHECK(n_impl.subtree_size == 1) + CARBON_CHECK(n_impl.subtree_size == 1) << "Subtree size must always be a positive integer!"; output << "}"; int next_depth = node_stack.empty() ? 0 : node_stack.back().second; - CHECK(next_depth <= depth) << "Cannot have the next depth increase!"; + CARBON_CHECK(next_depth <= depth) << "Cannot have the next depth increase!"; for (int close_children_count : llvm::seq(0, depth - next_depth)) { (void)close_children_count; output << "]}"; diff --git a/toolchain/parser/parse_tree_fuzzer.cpp b/toolchain/parser/parse_tree_fuzzer.cpp index 9b0dabe946c9..1c94b97420a6 100644 --- a/toolchain/parser/parse_tree_fuzzer.cpp +++ b/toolchain/parser/parse_tree_fuzzer.cpp @@ -42,7 +42,8 @@ extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, // In the absence of parse errors, we should have exactly as many nodes as // tokens. - CHECK(tree.size() == tokens.size()) << "Unexpected number of tree nodes!"; + CARBON_CHECK(tree.size() == tokens.size()) + << "Unexpected number of tree nodes!"; return 0; } diff --git a/toolchain/parser/parser_impl.cpp b/toolchain/parser/parser_impl.cpp index 6a1637d99ad1..ae44ac1bd93f 100644 --- a/toolchain/parser/parser_impl.cpp +++ b/toolchain/parser/parser_impl.cpp @@ -46,10 +46,10 @@ class ParseTree::Parser::ScopedStackStep { // Encapsulates checking the stack and erroring if needed. This should be called // at the start of every parse function. -#define RETURN_IF_STACK_LIMITED(error_return_expr) \ - ScopedStackStep scoped_stack_step(this); \ - if (!scoped_stack_step.VerifyUnderLimit()) { \ - return (error_return_expr); \ +#define CARBON_RETURN_IF_STACK_LIMITED(error_return_expr) \ + ScopedStackStep scoped_stack_step(this); \ + if (!scoped_stack_step.VerifyUnderLimit()) { \ + return (error_return_expr); \ } // A relative location for characters in errors. @@ -83,10 +83,11 @@ ParseTree::Parser::Parser(ParseTree& tree_arg, TokenizedBuffer& tokens_arg, emitter_(emitter), position_(tokens_.tokens().begin()), end_(tokens_.tokens().end()) { - CHECK(std::find_if(position_, end_, - [&](TokenizedBuffer::Token t) { - return tokens_.GetKind(t) == TokenKind::EndOfFile(); - }) != end_) + CARBON_CHECK(std::find_if(position_, end_, + [&](TokenizedBuffer::Token t) { + return tokens_.GetKind(t) == + TokenKind::EndOfFile(); + }) != end_) << "No EndOfFileToken in token buffer."; } @@ -110,16 +111,17 @@ auto ParseTree::Parser::Parse(TokenizedBuffer& tokens, parser.AddLeafNode(ParseNodeKind::FileEnd(), *parser.position_); - CHECK(tree.Verify()) << "Parse tree built but does not verify!"; + CARBON_CHECK(tree.Verify()) << "Parse tree built but does not verify!"; return tree; } auto ParseTree::Parser::Consume(TokenKind kind) -> TokenizedBuffer::Token { - CHECK(kind != TokenKind::EndOfFile()) << "Cannot consume the EOF token!"; - CHECK(NextTokenIs(kind)) << "The current token is the wrong kind!"; + CARBON_CHECK(kind != TokenKind::EndOfFile()) + << "Cannot consume the EOF token!"; + CARBON_CHECK(NextTokenIs(kind)) << "The current token is the wrong kind!"; TokenizedBuffer::Token t = *position_; ++position_; - CHECK(position_ != end_) + CARBON_CHECK(position_ != end_) << "Reached end of tokens without finding EOF token."; return t; } @@ -196,9 +198,9 @@ auto ParseTree::Parser::SkipMatchingGroup() -> bool { } auto ParseTree::Parser::SkipTo(TokenizedBuffer::Token t) -> void { - CHECK(t >= *position_) << "Tried to skip backwards."; + CARBON_CHECK(t >= *position_) << "Tried to skip backwards."; position_ = TokenizedBuffer::TokenIterator(t); - CHECK(position_ != end_) << "Skipped past EOF."; + CARBON_CHECK(position_ != end_) << "Skipped past EOF."; } auto ParseTree::Parser::FindNextOf( @@ -325,7 +327,7 @@ auto ParseTree::Parser::ParseList(TokenKind open, TokenKind close, auto end_of_element = FindNextOf({TokenKind::Comma(), close}); // The lexer guarantees that parentheses are balanced. - CHECK(end_of_element) << "missing matching `)` for `(`"; + CARBON_CHECK(end_of_element) << "missing matching `)` for `(`"; SkipTo(*end_of_element); } @@ -347,7 +349,7 @@ auto ParseTree::Parser::ParseList(TokenKind open, TokenKind close, } auto ParseTree::Parser::ParsePattern(PatternKind kind) -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); if (NextTokenIs(TokenKind::Identifier()) && tokens_.GetKind(*(position_ + 1)) == TokenKind::Colon()) { // identifier `:` type @@ -378,12 +380,12 @@ auto ParseTree::Parser::ParsePattern(PatternKind kind) -> llvm::Optional { } auto ParseTree::Parser::ParseFunctionParameter() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); return ParsePattern(PatternKind::Parameter); } auto ParseTree::Parser::ParseFunctionSignature() -> bool { - RETURN_IF_STACK_LIMITED(false); + CARBON_RETURN_IF_STACK_LIMITED(false); auto start = GetSubtreeStartPosition(); auto params = ParseParenList( @@ -410,7 +412,7 @@ auto ParseTree::Parser::ParseFunctionSignature() -> bool { } auto ParseTree::Parser::ParseCodeBlock() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); llvm::Optional maybe_open_curly = ConsumeIf(TokenKind::OpenCurlyBrace()); if (!maybe_open_curly) { @@ -454,7 +456,7 @@ auto ParseTree::Parser::ParseFunctionDeclaration() -> Node { return AddNode(ParseNodeKind::FunctionDeclaration(), function_intro_token, start, /*has_error=*/true); }; - RETURN_IF_STACK_LIMITED(add_error_function_node()); + CARBON_RETURN_IF_STACK_LIMITED(add_error_function_node()); auto handle_semi_in_error_recovery = [&](TokenizedBuffer::Token semi) { return AddLeafNode(ParseNodeKind::DeclarationEnd(), semi); @@ -519,9 +521,9 @@ auto ParseTree::Parser::ParseVariableDeclaration() -> Node { TokenizedBuffer::Token var_token = Consume(TokenKind::Var()); auto start = GetSubtreeStartPosition(); - RETURN_IF_STACK_LIMITED(AddNode(ParseNodeKind::VariableDeclaration(), - var_token, start, - /*has_error=*/true)); + CARBON_RETURN_IF_STACK_LIMITED(AddNode(ParseNodeKind::VariableDeclaration(), + var_token, start, + /*has_error=*/true)); auto pattern = ParsePattern(PatternKind::Variable); if (!pattern) { @@ -557,7 +559,7 @@ auto ParseTree::Parser::ParseEmptyDeclaration() -> Node { } auto ParseTree::Parser::ParseDeclaration() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); switch (NextTokenKind()) { case TokenKind::Fn(): return ParseFunctionDeclaration(); @@ -592,7 +594,7 @@ auto ParseTree::Parser::ParseDeclaration() -> llvm::Optional { } auto ParseTree::Parser::ParseParenExpression() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); // parenthesized-expression ::= `(` expression `)` // tuple-literal ::= `(` `)` // ::= `(` expression `,` [expression-list [`,`]] `)` @@ -615,7 +617,7 @@ auto ParseTree::Parser::ParseParenExpression() -> llvm::Optional { } auto ParseTree::Parser::ParseBraceExpression() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); // braced-expression ::= `{` [field-value-list] `}` // ::= `{` field-type-list `}` // field-value-list ::= field-value [`,`] @@ -695,7 +697,7 @@ auto ParseTree::Parser::ParseBraceExpression() -> llvm::Optional { } auto ParseTree::Parser::ParsePrimaryExpression() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); llvm::Optional kind; switch (NextTokenKind()) { case TokenKind::Identifier(): @@ -756,7 +758,7 @@ auto ParseTree::Parser::ParseDesignatorExpression(SubtreeStart start, auto ParseTree::Parser::ParseCallExpression(SubtreeStart start, bool has_errors) -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); // `(` expression-list[opt] `)` // // expression-list ::= expression @@ -772,7 +774,7 @@ auto ParseTree::Parser::ParseCallExpression(SubtreeStart start, bool has_errors) } auto ParseTree::Parser::ParsePostfixExpression() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); auto start = GetSubtreeStartPosition(); llvm::Optional expression = ParsePrimaryExpression(); @@ -794,7 +796,7 @@ auto ParseTree::Parser::ParsePostfixExpression() -> llvm::Optional { // This is subject to an infinite loop if a child call fails, so monitor for // stalling. if (last_position == position_) { - CHECK(expression == llvm::None); + CARBON_CHECK(expression == llvm::None); return expression; } last_position = position_; @@ -828,7 +830,7 @@ static auto IsPossibleStartOfOperand(TokenKind kind) -> bool { } auto ParseTree::Parser::IsLexicallyValidInfixOperator() -> bool { - CHECK(!AtEndOfFile()) << "Expected an operator token."; + CARBON_CHECK(!AtEndOfFile()) << "Expected an operator token."; bool leading_space = tokens_.HasLeadingWhitespace(*position_); bool trailing_space = tokens_.HasTrailingWhitespace(*position_); @@ -929,7 +931,7 @@ auto ParseTree::Parser::ParseOperatorExpression( OperatorRequiresParentheses, Error, "Parentheses are required to disambiguate operator precedence."); - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); auto start = GetSubtreeStartPosition(); llvm::Optional lhs; @@ -1002,17 +1004,17 @@ auto ParseTree::Parser::ParseOperatorExpression( } auto ParseTree::Parser::ParseExpression() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); return ParseOperatorExpression(PrecedenceGroup::ForTopLevelExpression()); } auto ParseTree::Parser::ParseType() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); return ParseOperatorExpression(PrecedenceGroup::ForType()); } auto ParseTree::Parser::ParseExpressionStatement() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); TokenizedBuffer::Token start_token = *position_; auto start = GetSubtreeStartPosition(); @@ -1041,7 +1043,7 @@ auto ParseTree::Parser::ParseExpressionStatement() -> llvm::Optional { auto ParseTree::Parser::ParseParenCondition(TokenKind introducer) -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); // `(` expression `)` auto start = GetSubtreeStartPosition(); auto open_paren = ConsumeIf(TokenKind::OpenParen()); @@ -1085,7 +1087,7 @@ auto ParseTree::Parser::ParseIfStatement() -> llvm::Optional { } auto ParseTree::Parser::ParseWhileStatement() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); auto start = GetSubtreeStartPosition(); auto while_token = Consume(TokenKind::While()); auto cond = ParseParenCondition(TokenKind::While()); @@ -1097,9 +1099,9 @@ auto ParseTree::Parser::ParseWhileStatement() -> llvm::Optional { auto ParseTree::Parser::ParseKeywordStatement(ParseNodeKind kind, KeywordStatementArgument argument) -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); auto keyword_kind = NextTokenKind(); - CHECK(keyword_kind.IsKeyword()); + CARBON_CHECK(keyword_kind.IsKeyword()); auto start = GetSubtreeStartPosition(); auto keyword = Consume(keyword_kind); @@ -1123,7 +1125,7 @@ auto ParseTree::Parser::ParseKeywordStatement(ParseNodeKind kind, } auto ParseTree::Parser::ParseStatement() -> llvm::Optional { - RETURN_IF_STACK_LIMITED(llvm::None); + CARBON_RETURN_IF_STACK_LIMITED(llvm::None); switch (NextTokenKind()) { case TokenKind::Var(): return ParseVariableDeclaration(); diff --git a/toolchain/parser/precedence.cpp b/toolchain/parser/precedence.cpp index 0b61c4d97a43..26a8706b0853 100644 --- a/toolchain/parser/precedence.cpp +++ b/toolchain/parser/precedence.cpp @@ -122,7 +122,7 @@ struct OperatorPriorityTable { for (int8_t a = 0; a != NumPrecedenceLevels; ++a) { for (int8_t b = 0; b != NumPrecedenceLevels; ++b) { if (table[a][b] == OperatorPriority::LeftFirst) { - CHECK(table[b][a] != OperatorPriority::LeftFirst) + CARBON_CHECK(table[b][a] != OperatorPriority::LeftFirst) << "inconsistent lookup table entries"; table[b][a] = OperatorPriority::RightFirst; } @@ -165,13 +165,13 @@ struct OperatorPriorityTable { constexpr void ConsistencyCheck() { for (int8_t level = 0; level != NumPrecedenceLevels; ++level) { if (level != Highest) { - CHECK(table[Highest][level] == OperatorPriority::LeftFirst && - table[level][Highest] == OperatorPriority::RightFirst) + CARBON_CHECK(table[Highest][level] == OperatorPriority::LeftFirst && + table[level][Highest] == OperatorPriority::RightFirst) << "Highest is not highest priority"; } if (level != Lowest) { - CHECK(table[Lowest][level] == OperatorPriority::RightFirst && - table[level][Lowest] == OperatorPriority::LeftFirst) + CARBON_CHECK(table[Lowest][level] == OperatorPriority::RightFirst && + table[level][Lowest] == OperatorPriority::LeftFirst) << "Lowest is not lowest priority"; } } diff --git a/toolchain/semantics/semantics_ir_factory.cpp b/toolchain/semantics/semantics_ir_factory.cpp index 59fad4152361..8a8f4d61d12b 100644 --- a/toolchain/semantics/semantics_ir_factory.cpp +++ b/toolchain/semantics/semantics_ir_factory.cpp @@ -27,8 +27,8 @@ void SemanticsIRFactory::ProcessRoots() { // No action needed. break; default: - FATAL() << "Unhandled node kind: " - << semantics_.parse_tree_->node_kind(node).name(); + CARBON_FATAL() << "Unhandled node kind: " + << semantics_.parse_tree_->node_kind(node).name(); } } } @@ -49,8 +49,8 @@ void SemanticsIRFactory::ProcessFunctionNode(SemanticsIR::Block& block, // TODO: Should accumulate the definition into the code block. break; default: - FATAL() << "Unhandled node kind: " - << semantics_.parse_tree_->node_kind(node).name(); + CARBON_FATAL() << "Unhandled node kind: " + << semantics_.parse_tree_->node_kind(node).name(); } } } diff --git a/toolchain/source/source_buffer.cpp b/toolchain/source/source_buffer.cpp index 3babd50c6d48..d123d96de640 100644 --- a/toolchain/source/source_buffer.cpp +++ b/toolchain/source/source_buffer.cpp @@ -124,7 +124,7 @@ SourceBuffer::SourceBuffer(std::string filename, llvm::StringRef text) : content_mode_(ContentMode::MMapped), filename_(std::move(filename)), text_(text) { - CHECK(!text.empty()) + CARBON_CHECK(!text.empty()) << "Must not have an empty text when we have mapped data from a file!"; } @@ -134,7 +134,7 @@ SourceBuffer::~SourceBuffer() { int result = munmap(const_cast(static_cast(text_.data())), text_.size()); - CHECK(result != -1) << "Unmapping text failed!"; + CARBON_CHECK(result != -1) << "Unmapping text failed!"; } }