diff --git a/common/BUILD b/common/BUILD index caa791112a51..95aabaeb49a9 100644 --- a/common/BUILD +++ b/common/BUILD @@ -34,6 +34,31 @@ cc_test( ], ) +cc_library( + name = "enum_base", + hdrs = ["enum_base.h"], + deps = [ + "//common:ostream", + "@llvm-project//llvm:Support", + ], +) + +cc_library( + name = "enum_base_test_def", + textual_hdrs = ["enum_base_test.def"], +) + +cc_test( + name = "enum_base_test", + srcs = ["enum_base_test.cpp"], + deps = [ + ":enum_base", + ":enum_base_test_def", + "//common:gtest_main", + "@com_google_googletest//:gtest", + ], +) + cc_library( name = "error", hdrs = ["error.h"], diff --git a/toolchain/common/enum_base.h b/common/enum_base.h similarity index 96% rename from toolchain/common/enum_base.h rename to common/enum_base.h index 9aa8950fd300..e4dd1a829c86 100644 --- a/toolchain/common/enum_base.h +++ b/common/enum_base.h @@ -2,8 +2,8 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#ifndef CARBON_TOOLCHAIN_COMMON_ENUM_BASE_H_ -#define CARBON_TOOLCHAIN_COMMON_ENUM_BASE_H_ +#ifndef CARBON_COMMON_ENUM_BASE_H_ +#define CARBON_COMMON_ENUM_BASE_H_ #include @@ -31,24 +31,24 @@ namespace Carbon::Internal { // ``` // CARBON_DEFINE_RAW_ENUM_CLASS(MyKind, uint8_t) { // #define CARBON_MY_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name) -// #include "toolchain/.../my_kind.def" +// #include ".../my_kind.def" // }; // // class MyKind : public CARBON_ENUM_BASE(MyKind) { // public: // #define CARBON_MY_KIND(Name) CARBON_ENUM_CONSTANT_DECLARATION(Name) -// #include "toolchain/.../my_kind.def" +// #include ".../my_kind.def" // }; // // #define CARBON_MY_KIND(Name) CARBON_ENUM_CONSTANT_DEFINITION(MyKind, Name) -// #include "toolchain/.../my_kind.def" +// #include ".../my_kind.def" // ``` // // In `my_kind.cpp`: // ``` // CARBON_DEFINE_ENUM_CLASS_NAMES(MyKind) = { // #define CARBON_MY_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name) -// #include "toolchain/.../my_kind.def" +// #include ".../my_kind.def" // }; // ``` template @@ -181,4 +181,4 @@ class EnumBase { // name. #define CARBON_ENUM_CLASS_NAME_STRING(Name) #Name, -#endif // CARBON_TOOLCHAIN_COMMON_ENUM_BASE_H_ +#endif // CARBON_COMMON_ENUM_BASE_H_ diff --git a/toolchain/common/enum_base_test.cpp b/common/enum_base_test.cpp similarity index 92% rename from toolchain/common/enum_base_test.cpp rename to common/enum_base_test.cpp index f6349e60c534..cf168f212f1e 100644 --- a/toolchain/common/enum_base_test.cpp +++ b/common/enum_base_test.cpp @@ -2,7 +2,7 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include "toolchain/common/enum_base.h" +#include "common/enum_base.h" #include @@ -10,13 +10,13 @@ namespace Carbon { CARBON_DEFINE_RAW_ENUM_CLASS(TestKind, uint8_t) { #define CARBON_ENUM_BASE_TEST_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name) -#include "toolchain/common/enum_base_test.def" +#include "common/enum_base_test.def" }; class TestKind : public CARBON_ENUM_BASE(TestKind) { public: #define CARBON_ENUM_BASE_TEST_KIND(Name) CARBON_ENUM_CONSTANT_DECLARATION(Name) -#include "toolchain/common/enum_base_test.def" +#include "common/enum_base_test.def" using EnumBase::AsInt; using EnumBase::FromInt; @@ -24,11 +24,11 @@ class TestKind : public CARBON_ENUM_BASE(TestKind) { #define CARBON_ENUM_BASE_TEST_KIND(Name) \ CARBON_ENUM_CONSTANT_DEFINITION(TestKind, Name) -#include "toolchain/common/enum_base_test.def" +#include "common/enum_base_test.def" CARBON_DEFINE_ENUM_CLASS_NAMES(TestKind) = { #define CARBON_ENUM_BASE_TEST_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name) -#include "toolchain/common/enum_base_test.def" +#include "common/enum_base_test.def" }; namespace { diff --git a/toolchain/common/enum_base_test.def b/common/enum_base_test.def similarity index 100% rename from toolchain/common/enum_base_test.def rename to common/enum_base_test.def diff --git a/explorer/interpreter/BUILD b/explorer/interpreter/BUILD index 277a22b99ec6..fdabb67b9e9f 100644 --- a/explorer/interpreter/BUILD +++ b/explorer/interpreter/BUILD @@ -205,6 +205,9 @@ cc_library( "matching_impl_set.h", "type_checker.h", ], + textual_hdrs = [ + "builtins.def", + ], deps = [ ":action", ":dictionary", @@ -212,6 +215,7 @@ cc_library( ":pattern_analysis", ":trace_stream", "//common:check", + "//common:enum_base", "//common:error", "//common:ostream", "//explorer/ast", diff --git a/explorer/interpreter/builtins.cpp b/explorer/interpreter/builtins.cpp index 421dccb14319..495b821899b2 100644 --- a/explorer/interpreter/builtins.cpp +++ b/explorer/interpreter/builtins.cpp @@ -10,6 +10,11 @@ using llvm::dyn_cast; namespace Carbon { +CARBON_DEFINE_ENUM_CLASS_NAMES(Builtin) = { +#define CARBON_BUILTIN(Name) CARBON_ENUM_CLASS_NAME_STRING(Name) +#include "explorer/interpreter/builtins.def" +}; + void Builtins::Register(Nonnull decl) { if (const auto* interface = dyn_cast(decl)) { if (interface->name().is_qualified()) { @@ -18,8 +23,8 @@ void Builtins::Register(Nonnull decl) { static std::map>* builtin_indexes = [] { std::map> builtin_indexes; - for (int index = 0; index <= static_cast(Builtin::Last); ++index) { - builtin_indexes.emplace(BuiltinNames[index], index); + for (int index = 0; index <= Builtin::NumBuiltins; ++index) { + builtin_indexes.emplace(Builtin::FromInt(index).name(), index); } return new auto(std::move(builtin_indexes)); }(); @@ -33,11 +38,10 @@ void Builtins::Register(Nonnull decl) { auto Builtins::Get(SourceLocation source_loc, Builtin builtin) const -> ErrorOr> { - std::optional result = - builtins_[static_cast(builtin)]; + std::optional result = builtins_[builtin.AsInt()]; if (!result.has_value()) { return ProgramError(source_loc) - << "missing declaration for builtin `" << GetName(builtin) << "`"; + << "missing declaration for builtin `" << builtin << "`"; } return result.value(); } diff --git a/explorer/interpreter/builtins.def b/explorer/interpreter/builtins.def new file mode 100644 index 000000000000..415db132dcd2 --- /dev/null +++ b/explorer/interpreter/builtins.def @@ -0,0 +1,68 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// This is an X-macro header. It does not use `#include` guards, and instead is +// designed to be `#include`ed after the x-macro is defined in order for its +// inclusion to expand to the desired output. Macro definitions are cleaned up +// at the end of this file. +// +// Supported x-macros are: +// - CARBON_BUILTIN(Name) +// Defines a builtin. + +#ifndef CARBON_BUILTIN +#error "Must define the x-macro to use this file." +#endif + +// Conversions. +CARBON_BUILTIN(As) +CARBON_BUILTIN(ImplicitAs) + +// Comparison. +CARBON_BUILTIN(EqWith) +CARBON_BUILTIN(LessWith) +CARBON_BUILTIN(LessEqWith) +CARBON_BUILTIN(GreaterWith) +CARBON_BUILTIN(GreaterEqWith) +CARBON_BUILTIN(CompareWith) + +// Arithmetic. +CARBON_BUILTIN(Negate) +CARBON_BUILTIN(AddWith) +CARBON_BUILTIN(SubWith) +CARBON_BUILTIN(MulWith) +CARBON_BUILTIN(DivWith) +CARBON_BUILTIN(ModWith) + +// Bitwise and shift. +CARBON_BUILTIN(BitComplement) +CARBON_BUILTIN(BitAndWith) +CARBON_BUILTIN(BitOrWith) +CARBON_BUILTIN(BitXorWith) +CARBON_BUILTIN(LeftShiftWith) +CARBON_BUILTIN(RightShiftWith) + +// Simple assignment. +CARBON_BUILTIN(AssignWith) + +// Compound assignment. +CARBON_BUILTIN(AddAssignWith) +CARBON_BUILTIN(SubAssignWith) +CARBON_BUILTIN(MulAssignWith) +CARBON_BUILTIN(DivAssignWith) +CARBON_BUILTIN(ModAssignWith) +CARBON_BUILTIN(BitAndAssignWith) +CARBON_BUILTIN(BitOrAssignWith) +CARBON_BUILTIN(BitXorAssignWith) +CARBON_BUILTIN(LeftShiftAssignWith) +CARBON_BUILTIN(RightShiftAssignWith) + +// Increment and decrement. +CARBON_BUILTIN(Inc) +CARBON_BUILTIN(Dec) + +// Last for the number of builtins. +CARBON_BUILTIN(Invalid) + +#undef CARBON_BUILTIN diff --git a/explorer/interpreter/builtins.h b/explorer/interpreter/builtins.h index c2fcdf72de4e..180ae271d741 100644 --- a/explorer/interpreter/builtins.h +++ b/explorer/interpreter/builtins.h @@ -9,6 +9,7 @@ #include #include +#include "common/enum_base.h" #include "common/error.h" #include "explorer/ast/declaration.h" #include "explorer/ast/expression.h" @@ -18,95 +19,32 @@ namespace Carbon { +CARBON_DEFINE_RAW_ENUM_CLASS(Builtin, int) { +#define CARBON_BUILTIN(Name) CARBON_RAW_ENUM_ENUMERATOR(Name) +#include "explorer/interpreter/builtins.def" +}; + +class Builtin : public CARBON_ENUM_BASE(Builtin) { + public: +#define CARBON_BUILTIN(Name) CARBON_ENUM_CONSTANT_DECLARATION(Name) +#include "explorer/interpreter/builtins.def" + + static const int NumBuiltins; + + // Support conversion to and from an int for array indexing. + using EnumBase::AsInt; + using EnumBase::FromInt; +}; + +#define CARBON_BUILTIN(Name) CARBON_ENUM_CONSTANT_DEFINITION(Builtin, Name) +#include "explorer/interpreter/builtins.def" + +constexpr int Builtin::NumBuiltins = Invalid.AsInt(); + class Builtins { public: explicit Builtins() = default; - enum class Builtin { - // Conversions. - As, - ImplicitAs, - - // Comparison. - EqWith, - LessWith, - LessEqWith, - GreaterWith, - GreaterEqWith, - CompareWith, - - // Arithmetic. - Negate, - AddWith, - SubWith, - MulWith, - DivWith, - ModWith, - - // Bitwise and shift. - BitComplement, - BitAndWith, - BitOrWith, - BitXorWith, - LeftShiftWith, - RightShiftWith, - - // Simple assignment. - AssignWith, - - // Compound assignment. - AddAssignWith, - SubAssignWith, - MulAssignWith, - DivAssignWith, - ModAssignWith, - BitAndAssignWith, - BitOrAssignWith, - BitXorAssignWith, - LeftShiftAssignWith, - RightShiftAssignWith, - - // Increment and decrement. - Inc, - Dec, - - Last = Dec - }; - // TODO: In C++20, replace with `using enum Builtin;`. - static constexpr Builtin As = Builtin::As; - static constexpr Builtin ImplicitAs = Builtin::ImplicitAs; - static constexpr Builtin EqWith = Builtin::EqWith; - static constexpr Builtin LessWith = Builtin::LessWith; - static constexpr Builtin LessEqWith = Builtin::LessEqWith; - static constexpr Builtin GreaterWith = Builtin::GreaterWith; - static constexpr Builtin GreaterEqWith = Builtin::GreaterEqWith; - static constexpr Builtin CompareWith = Builtin::CompareWith; - static constexpr Builtin Negate = Builtin::Negate; - static constexpr Builtin AddWith = Builtin::AddWith; - static constexpr Builtin SubWith = Builtin::SubWith; - static constexpr Builtin MulWith = Builtin::MulWith; - static constexpr Builtin DivWith = Builtin::DivWith; - static constexpr Builtin ModWith = Builtin::ModWith; - static constexpr Builtin BitComplement = Builtin::BitComplement; - static constexpr Builtin BitAndWith = Builtin::BitAndWith; - static constexpr Builtin BitOrWith = Builtin::BitOrWith; - static constexpr Builtin BitXorWith = Builtin::BitXorWith; - static constexpr Builtin LeftShiftWith = Builtin::LeftShiftWith; - static constexpr Builtin RightShiftWith = Builtin::RightShiftWith; - static constexpr Builtin AssignWith = Builtin::AssignWith; - static constexpr Builtin AddAssignWith = Builtin::AddAssignWith; - static constexpr Builtin SubAssignWith = Builtin::SubAssignWith; - static constexpr Builtin MulAssignWith = Builtin::MulAssignWith; - static constexpr Builtin DivAssignWith = Builtin::DivAssignWith; - static constexpr Builtin ModAssignWith = Builtin::ModAssignWith; - static constexpr Builtin BitAndAssignWith = Builtin::BitAndAssignWith; - static constexpr Builtin BitOrAssignWith = Builtin::BitOrAssignWith; - static constexpr Builtin BitXorAssignWith = Builtin::BitXorAssignWith; - static constexpr Builtin LeftShiftAssignWith = Builtin::LeftShiftAssignWith; - static constexpr Builtin RightShiftAssignWith = Builtin::RightShiftAssignWith; - static constexpr Builtin Inc = Builtin::Inc; - static constexpr Builtin Dec = Builtin::Dec; - // Register a declaration that might be a builtin. void Register(Nonnull decl); @@ -114,49 +52,9 @@ class Builtins { auto Get(SourceLocation source_loc, Builtin builtin) const -> ErrorOr>; - // Get the source name of a builtin. - static constexpr auto GetName(Builtin builtin) -> std::string_view { - return BuiltinNames[static_cast(builtin)]; - } - private: - static constexpr int NumBuiltins = static_cast(Builtin::Last) + 1; - static constexpr const char* BuiltinNames[NumBuiltins] = { - "As", - "ImplicitAs", - "EqWith", - "LessWith", - "LessEqWith", - "GreaterWith", - "GreaterEqWith", - "CompareWith", - "Negate", - "AddWith", - "SubWith", - "MulWith", - "DivWith", - "ModWith", - "BitComplement", - "BitAndWith", - "BitOrWith", - "BitXorWith", - "LeftShiftWith", - "RightShiftWith", - "AssignWith", - "AddAssignWith", - "SubAssignWith", - "MulAssignWith", - "DivAssignWith", - "ModAssignWith", - "BitAndAssignWith", - "BitOrAssignWith", - "BitXorAssignWith", - "LeftShiftAssignWith", - "RightShiftAssignWith", - "Inc", - "Dec"}; - - std::optional> builtins_[NumBuiltins] = {}; + std::optional> builtins_[Builtin::NumBuiltins] = + {}; }; } // namespace Carbon diff --git a/explorer/interpreter/type_checker.cpp b/explorer/interpreter/type_checker.cpp index 8e9a47c2e897..39e559c63f63 100644 --- a/explorer/interpreter/type_checker.cpp +++ b/explorer/interpreter/type_checker.cpp @@ -610,7 +610,7 @@ auto TypeChecker::IsImplicitlyConvertible( // We didn't find a builtin implicit conversion. Try a user-defined one. SourceLocation source_loc = SourceLocation::DiagnosticsIgnored(); ErrorOr> iface_type = GetBuiltinInterfaceType( - source_loc, BuiltinInterfaceName{Builtins::ImplicitAs, destination}); + source_loc, BuiltinInterfaceName{Builtin::ImplicitAs, destination}); // TODO: If the Resolve call fails with a hard error, don't swallow it. return iface_type.ok() && impl_scope.Resolve(*iface_type, source, source_loc, *this).ok(); @@ -722,7 +722,7 @@ auto TypeChecker::ImplicitlyConvert(std::string_view context, ErrorOr> converted = BuildBuiltinMethodCall( impl_scope, source, - BuiltinInterfaceName{Builtins::ImplicitAs, destination}, + BuiltinInterfaceName{Builtin::ImplicitAs, destination}, BuiltinMethodCall{"Convert"}); if (!converted.ok()) { // We couldn't find a matching `impl`. @@ -752,9 +752,8 @@ auto TypeChecker::GetBuiltinInterfaceType(SourceLocation source_loc, BuiltinInterfaceName interface) const -> ErrorOr> { auto bad_builtin = [&]() -> Error { - return ProgramError(source_loc) - << "unsupported declaration for builtin `" - << Builtins::GetName(interface.builtin) << "`"; + return ProgramError(source_loc) << "unsupported declaration for builtin `" + << interface.builtin << "`"; }; // Find the builtin interface declaration. @@ -795,7 +794,7 @@ auto TypeChecker::BuildBuiltinMethodCall(const ImplScope& impl_scope, CARBON_ASSIGN_OR_RETURN(Nonnull iface_type, GetBuiltinInterfaceType(source_loc, interface)); - if (interface.builtin == Builtins::ImplicitAs) { + if (interface.builtin == Builtin::ImplicitAs) { // Type-checking the below expression resolves the member name to // `As(Destination).Convert`, which allows both implicit and explicit // conversions. So manually check that `ImplicitAs(Destination)` is @@ -3103,8 +3102,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, ts.push_back(&argument->static_type()); } - auto handle_unary_operator = - [&](Builtins::Builtin builtin) -> ErrorOr { + auto handle_unary_operator = [&](Builtin builtin) -> ErrorOr { ErrorOr> result = BuildBuiltinMethodCall( impl_scope, op.arguments()[0], BuiltinInterfaceName{builtin}, BuiltinMethodCall{"Op"}); @@ -3118,8 +3116,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, return Success(); }; - auto handle_binary_operator = - [&](Builtins::Builtin builtin) -> ErrorOr { + auto handle_binary_operator = [&](Builtin builtin) -> ErrorOr { ErrorOr> result = BuildBuiltinMethodCall( impl_scope, op.arguments()[0], BuiltinInterfaceName{builtin, ts[1]}, BuiltinMethodCall{"Op", {op.arguments()[1]}}); @@ -3133,8 +3130,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, return Success(); }; - auto handle_binary_arithmetic = - [&](Builtins::Builtin builtin) -> ErrorOr { + auto handle_binary_arithmetic = [&](Builtin builtin) -> ErrorOr { // Handle a built-in operator first. // TODO: Replace this with an intrinsic. if (isa(ts[0]) && isa(ts[1]) && @@ -3149,7 +3145,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, }; auto handle_compare = - [&](Builtins::Builtin builtin, const std::string& method_name, + [&](Builtin builtin, const std::string& method_name, const std::string_view& operator_desc) -> ErrorOr { ErrorOr> converted = BuildBuiltinMethodCall( impl_scope, op.arguments()[0], BuiltinInterfaceName{builtin, ts[1]}, @@ -3174,18 +3170,18 @@ auto TypeChecker::TypeCheckExp(Nonnull e, return Success(); } // Now try an overloaded negation. - return handle_unary_operator(Builtins::Negate); + return handle_unary_operator(Builtin::Negate); } case Operator::Add: - return handle_binary_arithmetic(Builtins::AddWith); + return handle_binary_arithmetic(Builtin::AddWith); case Operator::Sub: - return handle_binary_arithmetic(Builtins::SubWith); + return handle_binary_arithmetic(Builtin::SubWith); case Operator::Mul: - return handle_binary_arithmetic(Builtins::MulWith); + return handle_binary_arithmetic(Builtin::MulWith); case Operator::Div: - return handle_binary_arithmetic(Builtins::DivWith); + return handle_binary_arithmetic(Builtin::DivWith); case Operator::Mod: - return handle_binary_arithmetic(Builtins::ModWith); + return handle_binary_arithmetic(Builtin::ModWith); case Operator::BitwiseAnd: // `&` between type-of-types performs constraint combination. // TODO: Should this be done via an intrinsic? @@ -3213,17 +3209,17 @@ auto TypeChecker::TypeCheckExp(Nonnull e, ValueCategory::Let)); return Success(); } - return handle_binary_operator(Builtins::BitAndWith); + return handle_binary_operator(Builtin::BitAndWith); case Operator::BitwiseOr: - return handle_binary_operator(Builtins::BitOrWith); + return handle_binary_operator(Builtin::BitOrWith); case Operator::BitwiseXor: - return handle_binary_operator(Builtins::BitXorWith); + return handle_binary_operator(Builtin::BitXorWith); case Operator::BitShiftLeft: - return handle_binary_operator(Builtins::LeftShiftWith); + return handle_binary_operator(Builtin::LeftShiftWith); case Operator::BitShiftRight: - return handle_binary_operator(Builtins::RightShiftWith); + return handle_binary_operator(Builtin::RightShiftWith); case Operator::Complement: - return handle_unary_operator(Builtins::BitComplement); + return handle_unary_operator(Builtin::BitComplement); case Operator::And: CARBON_RETURN_IF_ERROR(ExpectExactType(e->source_loc(), "&&(1)", arena_->New(), ts[0], @@ -3252,18 +3248,18 @@ auto TypeChecker::TypeCheckExp(Nonnull e, op.set_value_category(ValueCategory::Let); return Success(); case Operator::Eq: - return handle_compare(Builtins::EqWith, "Equal", "equality"); + return handle_compare(Builtin::EqWith, "Equal", "equality"); case Operator::NotEq: - return handle_compare(Builtins::EqWith, "NotEqual", "equality"); + return handle_compare(Builtin::EqWith, "NotEqual", "equality"); case Operator::Less: - return handle_compare(Builtins::LessWith, "Less", "less"); + return handle_compare(Builtin::LessWith, "Less", "less"); case Operator::LessEq: - return handle_compare(Builtins::LessEqWith, "LessEq", "less equal"); + return handle_compare(Builtin::LessEqWith, "LessEq", "less equal"); case Operator::GreaterEq: - return handle_compare(Builtins::GreaterEqWith, "GreaterEq", + return handle_compare(Builtin::GreaterEqWith, "GreaterEq", "greater equal"); case Operator::Greater: - return handle_compare(Builtins::GreaterWith, "Greater", "greater"); + return handle_compare(Builtin::GreaterWith, "Greater", "greater"); case Operator::Deref: CARBON_RETURN_IF_ERROR( ExpectPointerType(e->source_loc(), "*", ts[0])); @@ -3296,7 +3292,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, TypeCheckTypeExp(op.arguments()[1], impl_scope)); ErrorOr> converted = BuildBuiltinMethodCall(impl_scope, op.arguments()[0], - BuiltinInterfaceName{Builtins::As, type}, + BuiltinInterfaceName{Builtin::As, type}, BuiltinMethodCall{"Convert"}); if (!converted.ok()) { // We couldn't find a matching `impl`. @@ -4189,31 +4185,30 @@ auto TypeChecker::TypeCheckGenericBinding(GenericBinding& binding, // Get the builtin interface that should be used for the given kind of // assignment operator. -static auto GetBuiltinInterfaceForAssignOperator(AssignOperator op) - -> Builtins::Builtin { +static auto GetBuiltinInterfaceForAssignOperator(AssignOperator op) -> Builtin { switch (op) { case AssignOperator::Plain: - return Builtins::AssignWith; + return Builtin::AssignWith; case AssignOperator::Add: - return Builtins::AddAssignWith; + return Builtin::AddAssignWith; case AssignOperator::Sub: - return Builtins::SubAssignWith; + return Builtin::SubAssignWith; case AssignOperator::Mul: - return Builtins::MulAssignWith; + return Builtin::MulAssignWith; case AssignOperator::Div: - return Builtins::DivAssignWith; + return Builtin::DivAssignWith; case AssignOperator::Mod: - return Builtins::ModAssignWith; + return Builtin::ModAssignWith; case AssignOperator::And: - return Builtins::BitAndAssignWith; + return Builtin::BitAndAssignWith; case AssignOperator::Or: - return Builtins::BitOrAssignWith; + return Builtin::BitOrAssignWith; case AssignOperator::Xor: - return Builtins::BitXorAssignWith; + return Builtin::BitXorAssignWith; case AssignOperator::ShiftLeft: - return Builtins::LeftShiftAssignWith; + return Builtin::LeftShiftAssignWith; case AssignOperator::ShiftRight: - return Builtins::RightShiftAssignWith; + return Builtin::RightShiftAssignWith; } } @@ -4388,7 +4383,7 @@ auto TypeChecker::TypeCheckStmt(Nonnull s, BuildBuiltinMethodCall( impl_scope, &inc_dec.argument(), BuiltinInterfaceName{ - inc_dec.is_increment() ? Builtins::Inc : Builtins::Dec, {}}, + inc_dec.is_increment() ? Builtin::Inc : Builtin::Dec, {}}, BuiltinMethodCall{"Op"})); inc_dec.set_rewritten_form(rewritten); return Success(); diff --git a/explorer/interpreter/type_checker.h b/explorer/interpreter/type_checker.h index 5fd11159b9d2..41e2a737be74 100644 --- a/explorer/interpreter/type_checker.h +++ b/explorer/interpreter/type_checker.h @@ -430,7 +430,7 @@ class TypeChecker { // The name of a builtin interface, with any arguments. struct BuiltinInterfaceName { - Builtins::Builtin builtin; + Builtin builtin; llvm::ArrayRef> arguments = {}; }; // The name of a method on a builtin interface, with any arguments. diff --git a/toolchain/common/BUILD b/toolchain/common/BUILD index 076e38e9bcc0..55aed41553ba 100644 --- a/toolchain/common/BUILD +++ b/toolchain/common/BUILD @@ -4,31 +4,6 @@ package(default_visibility = ["//visibility:public"]) -cc_library( - name = "enum_base", - hdrs = ["enum_base.h"], - deps = [ - "//common:ostream", - "@llvm-project//llvm:Support", - ], -) - -cc_library( - name = "enum_base_test_def", - textual_hdrs = ["enum_base_test.def"], -) - -cc_test( - name = "enum_base_test", - srcs = ["enum_base_test.cpp"], - deps = [ - ":enum_base", - ":enum_base_test_def", - "//common:gtest_main", - "@com_google_googletest//:gtest", - ], -) - cc_library( name = "index_base", hdrs = ["index_base.h"], diff --git a/toolchain/diagnostics/BUILD b/toolchain/diagnostics/BUILD index 3e723159dc61..4ed367dd831c 100644 --- a/toolchain/diagnostics/BUILD +++ b/toolchain/diagnostics/BUILD @@ -35,7 +35,7 @@ cc_library( "diagnostic_kind.def", ], deps = [ - "//toolchain/common:enum_base", + "//common:enum_base", ], ) diff --git a/toolchain/diagnostics/diagnostic_kind.h b/toolchain/diagnostics/diagnostic_kind.h index dd424ca8e3e0..24b196562e5e 100644 --- a/toolchain/diagnostics/diagnostic_kind.h +++ b/toolchain/diagnostics/diagnostic_kind.h @@ -7,7 +7,7 @@ #include -#include "toolchain/common/enum_base.h" +#include "common/enum_base.h" namespace Carbon { diff --git a/toolchain/lexer/BUILD b/toolchain/lexer/BUILD index 50733b3be7c5..6d03d70670d4 100644 --- a/toolchain/lexer/BUILD +++ b/toolchain/lexer/BUILD @@ -13,7 +13,7 @@ cc_library( textual_hdrs = ["token_kind.def"], deps = [ "//common:check", - "//toolchain/common:enum_base", + "//common:enum_base", "@llvm-project//llvm:Support", ], ) diff --git a/toolchain/lexer/token_kind.h b/toolchain/lexer/token_kind.h index 588341f6bc14..b206bc55c183 100644 --- a/toolchain/lexer/token_kind.h +++ b/toolchain/lexer/token_kind.h @@ -7,8 +7,8 @@ #include +#include "common/enum_base.h" #include "llvm/Support/FormatVariadicDetails.h" -#include "toolchain/common/enum_base.h" namespace Carbon { diff --git a/toolchain/parser/BUILD b/toolchain/parser/BUILD index 6898bf47750c..3f7b73d2c74a 100644 --- a/toolchain/parser/BUILD +++ b/toolchain/parser/BUILD @@ -13,7 +13,7 @@ cc_library( textual_hdrs = ["parse_node_kind.def"], deps = [ "//common:check", - "//toolchain/common:enum_base", + "//common:enum_base", ], ) @@ -22,7 +22,7 @@ cc_library( srcs = ["parser_state.cpp"], hdrs = ["parser_state.h"], textual_hdrs = ["parser_state.def"], - deps = ["//toolchain/common:enum_base"], + deps = ["//common:enum_base"], ) cc_library( diff --git a/toolchain/parser/parse_node_kind.h b/toolchain/parser/parse_node_kind.h index 1a5f4b85ca0d..90e52d681582 100644 --- a/toolchain/parser/parse_node_kind.h +++ b/toolchain/parser/parse_node_kind.h @@ -7,7 +7,7 @@ #include -#include "toolchain/common/enum_base.h" +#include "common/enum_base.h" namespace Carbon { diff --git a/toolchain/parser/parser_state.h b/toolchain/parser/parser_state.h index 42fd70fa0262..5c9729d70162 100644 --- a/toolchain/parser/parser_state.h +++ b/toolchain/parser/parser_state.h @@ -7,7 +7,7 @@ #include -#include "toolchain/common/enum_base.h" +#include "common/enum_base.h" namespace Carbon { diff --git a/toolchain/semantics/BUILD b/toolchain/semantics/BUILD index 1cb8e9801ad5..9e8761eadf34 100644 --- a/toolchain/semantics/BUILD +++ b/toolchain/semantics/BUILD @@ -9,7 +9,7 @@ cc_library( srcs = ["semantics_builtin_kind.cpp"], hdrs = ["semantics_builtin_kind.h"], textual_hdrs = ["semantics_builtin_kind.def"], - deps = ["//toolchain/common:enum_base"], + deps = ["//common:enum_base"], ) cc_library( @@ -17,7 +17,7 @@ cc_library( srcs = ["semantics_node_kind.cpp"], hdrs = ["semantics_node_kind.h"], textual_hdrs = ["semantics_node_kind.def"], - deps = ["//toolchain/common:enum_base"], + deps = ["//common:enum_base"], ) cc_library( diff --git a/toolchain/semantics/semantics_builtin_kind.h b/toolchain/semantics/semantics_builtin_kind.h index 408d9e56ac89..0ccba6fd5c9d 100644 --- a/toolchain/semantics/semantics_builtin_kind.h +++ b/toolchain/semantics/semantics_builtin_kind.h @@ -7,7 +7,7 @@ #include -#include "toolchain/common/enum_base.h" +#include "common/enum_base.h" namespace Carbon { diff --git a/toolchain/semantics/semantics_node_kind.h b/toolchain/semantics/semantics_node_kind.h index baf45401fc11..b90ad5712ae4 100644 --- a/toolchain/semantics/semantics_node_kind.h +++ b/toolchain/semantics/semantics_node_kind.h @@ -7,7 +7,7 @@ #include -#include "toolchain/common/enum_base.h" +#include "common/enum_base.h" namespace Carbon {