builtins.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_EXPLORER_INTERPRETER_BUILTINS_H_
  5. #define CARBON_EXPLORER_INTERPRETER_BUILTINS_H_
  6. #include <array>
  7. #include <optional>
  8. #include <string_view>
  9. #include "common/error.h"
  10. #include "explorer/ast/declaration.h"
  11. #include "explorer/ast/expression.h"
  12. #include "explorer/common/nonnull.h"
  13. #include "explorer/common/source_location.h"
  14. #include "explorer/interpreter/value.h"
  15. namespace Carbon {
  16. class Builtins {
  17. public:
  18. explicit Builtins() = default;
  19. enum class Builtin {
  20. // Conversions.
  21. As,
  22. ImplicitAs,
  23. // Comparison.
  24. EqWith,
  25. LessWith,
  26. LessEqWith,
  27. GreaterWith,
  28. GreaterEqWith,
  29. CompareWith,
  30. // Arithmetic.
  31. Negate,
  32. AddWith,
  33. SubWith,
  34. MulWith,
  35. DivWith,
  36. ModWith,
  37. // Bitwise and shift.
  38. BitComplement,
  39. BitAndWith,
  40. BitOrWith,
  41. BitXorWith,
  42. LeftShiftWith,
  43. RightShiftWith,
  44. Last = RightShiftWith
  45. };
  46. // TODO: In C++20, replace with `using enum Builtin;`.
  47. static constexpr Builtin As = Builtin::As;
  48. static constexpr Builtin ImplicitAs = Builtin::ImplicitAs;
  49. static constexpr Builtin EqWith = Builtin::EqWith;
  50. static constexpr Builtin LessWith = Builtin::LessWith;
  51. static constexpr Builtin LessEqWith = Builtin::LessEqWith;
  52. static constexpr Builtin GreaterWith = Builtin::GreaterWith;
  53. static constexpr Builtin GreaterEqWith = Builtin::GreaterEqWith;
  54. static constexpr Builtin Negate = Builtin::Negate;
  55. static constexpr Builtin AddWith = Builtin::AddWith;
  56. static constexpr Builtin SubWith = Builtin::SubWith;
  57. static constexpr Builtin MulWith = Builtin::MulWith;
  58. static constexpr Builtin DivWith = Builtin::DivWith;
  59. static constexpr Builtin ModWith = Builtin::ModWith;
  60. static constexpr Builtin BitComplement = Builtin::BitComplement;
  61. static constexpr Builtin BitAndWith = Builtin::BitAndWith;
  62. static constexpr Builtin BitOrWith = Builtin::BitOrWith;
  63. static constexpr Builtin BitXorWith = Builtin::BitXorWith;
  64. static constexpr Builtin LeftShiftWith = Builtin::LeftShiftWith;
  65. static constexpr Builtin RightShiftWith = Builtin::RightShiftWith;
  66. static constexpr Builtin CompareWith = Builtin::CompareWith;
  67. // Register a declaration that might be a builtin.
  68. void Register(Nonnull<const Declaration*> decl);
  69. // Get a registered builtin.
  70. auto Get(SourceLocation source_loc, Builtin builtin) const
  71. -> ErrorOr<Nonnull<const Declaration*>>;
  72. // Get the source name of a builtin.
  73. static constexpr auto GetName(Builtin builtin) -> std::string_view {
  74. return BuiltinNames[static_cast<int>(builtin)];
  75. }
  76. private:
  77. static constexpr int NumBuiltins = static_cast<int>(Builtin::Last) + 1;
  78. static constexpr const char* BuiltinNames[NumBuiltins] = {
  79. "As", "ImplicitAs", "EqWith", "LessWith",
  80. "LessEqWith", "GreaterWith", "GreaterEqWith", "CompareWith",
  81. "Negate", "AddWith", "SubWith", "MulWith",
  82. "DivWith", "ModWith", "BitComplement", "BitAndWith",
  83. "BitOrWith", "BitXorWith", "LeftShiftWith", "RightShiftWith"};
  84. std::optional<Nonnull<const Declaration*>> builtins_[NumBuiltins] = {};
  85. };
  86. } // namespace Carbon
  87. #endif // CARBON_EXPLORER_INTERPRETER_BUILTINS_H_