Sfoglia il codice sorgente

Reuse EnumBase for interpreter's Builtin enum (#2688)

This was bugging me after I saw all the strings; it feels like this is why we have EnumBase on the toolchain side.

I've included the move of EnumBase to //common because I figured it's reasonable to evaluate together; if we don't want EnumBase in this case, it doesn't make sense to move.
Jon Ross-Perkins 3 anni fa
parent
commit
9e1a5cfaee

+ 25 - 0
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"],

+ 7 - 7
toolchain/common/enum_base.h → 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 <type_traits>
 
@@ -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 <typename DerivedT, typename EnumT>
@@ -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_

+ 5 - 5
toolchain/common/enum_base_test.cpp → 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 <gtest/gtest.h>
 
@@ -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 {

+ 0 - 0
toolchain/common/enum_base_test.def → common/enum_base_test.def


+ 4 - 0
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",

+ 9 - 5
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<const Declaration*> decl) {
   if (const auto* interface = dyn_cast<InterfaceDeclaration>(decl)) {
     if (interface->name().is_qualified()) {
@@ -18,8 +23,8 @@ void Builtins::Register(Nonnull<const Declaration*> decl) {
 
     static std::map<std::string, int, std::less<>>* builtin_indexes = [] {
       std::map<std::string, int, std::less<>> builtin_indexes;
-      for (int index = 0; index <= static_cast<int>(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<const Declaration*> decl) {
 
 auto Builtins::Get(SourceLocation source_loc, Builtin builtin) const
     -> ErrorOr<Nonnull<const Declaration*>> {
-  std::optional<const Declaration*> result =
-      builtins_[static_cast<int>(builtin)];
+  std::optional<const Declaration*> 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();
 }

+ 68 - 0
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

+ 25 - 127
explorer/interpreter/builtins.h

@@ -9,6 +9,7 @@
 #include <optional>
 #include <string_view>
 
+#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<const Declaration*> decl);
 
@@ -114,49 +52,9 @@ class Builtins {
   auto Get(SourceLocation source_loc, Builtin builtin) const
       -> ErrorOr<Nonnull<const Declaration*>>;
 
-  // Get the source name of a builtin.
-  static constexpr auto GetName(Builtin builtin) -> std::string_view {
-    return BuiltinNames[static_cast<int>(builtin)];
-  }
-
  private:
-  static constexpr int NumBuiltins = static_cast<int>(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<Nonnull<const Declaration*>> builtins_[NumBuiltins] = {};
+  std::optional<Nonnull<const Declaration*>> builtins_[Builtin::NumBuiltins] =
+      {};
 };
 
 }  // namespace Carbon

+ 41 - 46
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<Nonnull<const InterfaceType*>> 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<Nonnull<Expression*>> 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<Nonnull<const InterfaceType*>> {
   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<const InterfaceType*> 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<Expression*> e,
         ts.push_back(&argument->static_type());
       }
 
-      auto handle_unary_operator =
-          [&](Builtins::Builtin builtin) -> ErrorOr<Success> {
+      auto handle_unary_operator = [&](Builtin builtin) -> ErrorOr<Success> {
         ErrorOr<Nonnull<Expression*>> result = BuildBuiltinMethodCall(
             impl_scope, op.arguments()[0], BuiltinInterfaceName{builtin},
             BuiltinMethodCall{"Op"});
@@ -3118,8 +3116,7 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
         return Success();
       };
 
-      auto handle_binary_operator =
-          [&](Builtins::Builtin builtin) -> ErrorOr<Success> {
+      auto handle_binary_operator = [&](Builtin builtin) -> ErrorOr<Success> {
         ErrorOr<Nonnull<Expression*>> result = BuildBuiltinMethodCall(
             impl_scope, op.arguments()[0], BuiltinInterfaceName{builtin, ts[1]},
             BuiltinMethodCall{"Op", {op.arguments()[1]}});
@@ -3133,8 +3130,7 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
         return Success();
       };
 
-      auto handle_binary_arithmetic =
-          [&](Builtins::Builtin builtin) -> ErrorOr<Success> {
+      auto handle_binary_arithmetic = [&](Builtin builtin) -> ErrorOr<Success> {
         // Handle a built-in operator first.
         // TODO: Replace this with an intrinsic.
         if (isa<IntType>(ts[0]) && isa<IntType>(ts[1]) &&
@@ -3149,7 +3145,7 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> 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<Success> {
         ErrorOr<Nonnull<Expression*>> converted = BuildBuiltinMethodCall(
             impl_scope, op.arguments()[0], BuiltinInterfaceName{builtin, ts[1]},
@@ -3174,18 +3170,18 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> 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<Expression*> 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<BoolType>(), ts[0],
@@ -3252,18 +3248,18 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> 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<Expression*> e,
               TypeCheckTypeExp(op.arguments()[1], impl_scope));
           ErrorOr<Nonnull<Expression*>> 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<Statement*> 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();

+ 1 - 1
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<Nonnull<const Value*>> arguments = {};
   };
   // The name of a method on a builtin interface, with any arguments.

+ 0 - 25
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"],

+ 1 - 1
toolchain/diagnostics/BUILD

@@ -35,7 +35,7 @@ cc_library(
         "diagnostic_kind.def",
     ],
     deps = [
-        "//toolchain/common:enum_base",
+        "//common:enum_base",
     ],
 )
 

+ 1 - 1
toolchain/diagnostics/diagnostic_kind.h

@@ -7,7 +7,7 @@
 
 #include <cstdint>
 
-#include "toolchain/common/enum_base.h"
+#include "common/enum_base.h"
 
 namespace Carbon {
 

+ 1 - 1
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",
     ],
 )

+ 1 - 1
toolchain/lexer/token_kind.h

@@ -7,8 +7,8 @@
 
 #include <cstdint>
 
+#include "common/enum_base.h"
 #include "llvm/Support/FormatVariadicDetails.h"
-#include "toolchain/common/enum_base.h"
 
 namespace Carbon {
 

+ 2 - 2
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(

+ 1 - 1
toolchain/parser/parse_node_kind.h

@@ -7,7 +7,7 @@
 
 #include <cstdint>
 
-#include "toolchain/common/enum_base.h"
+#include "common/enum_base.h"
 
 namespace Carbon {
 

+ 1 - 1
toolchain/parser/parser_state.h

@@ -7,7 +7,7 @@
 
 #include <cstdint>
 
-#include "toolchain/common/enum_base.h"
+#include "common/enum_base.h"
 
 namespace Carbon {
 

+ 2 - 2
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(

+ 1 - 1
toolchain/semantics/semantics_builtin_kind.h

@@ -7,7 +7,7 @@
 
 #include <cstdint>
 
-#include "toolchain/common/enum_base.h"
+#include "common/enum_base.h"
 
 namespace Carbon {
 

+ 1 - 1
toolchain/semantics/semantics_node_kind.h

@@ -7,7 +7,7 @@
 
 #include <cstdint>
 
-#include "toolchain/common/enum_base.h"
+#include "common/enum_base.h"
 
 namespace Carbon {