builtins.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/enum_base.h"
  10. #include "common/error.h"
  11. #include "explorer/ast/declaration.h"
  12. #include "explorer/ast/expression.h"
  13. #include "explorer/ast/value.h"
  14. #include "explorer/common/nonnull.h"
  15. #include "explorer/common/source_location.h"
  16. namespace Carbon {
  17. CARBON_DEFINE_RAW_ENUM_CLASS(Builtin, int) {
  18. #define CARBON_BUILTIN(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  19. #include "explorer/interpreter/builtins.def"
  20. };
  21. class Builtin : public CARBON_ENUM_BASE(Builtin) {
  22. public:
  23. #define CARBON_BUILTIN(Name) CARBON_ENUM_CONSTANT_DECLARATION(Name)
  24. #include "explorer/interpreter/builtins.def"
  25. static const int NumBuiltins;
  26. // Support conversion to and from an int for array indexing.
  27. using EnumBase::AsInt;
  28. using EnumBase::FromInt;
  29. };
  30. #define CARBON_BUILTIN(Name) CARBON_ENUM_CONSTANT_DEFINITION(Builtin, Name)
  31. #include "explorer/interpreter/builtins.def"
  32. constexpr int Builtin::NumBuiltins = Invalid.AsInt();
  33. class Builtins {
  34. public:
  35. explicit Builtins() = default;
  36. // Register a declaration that might be a builtin.
  37. void Register(Nonnull<const Declaration*> decl);
  38. // Get a registered builtin.
  39. auto Get(SourceLocation source_loc, Builtin builtin) const
  40. -> ErrorOr<Nonnull<const Declaration*>>;
  41. private:
  42. std::optional<Nonnull<const Declaration*>> builtins_[Builtin::NumBuiltins] =
  43. {};
  44. };
  45. } // namespace Carbon
  46. #endif // CARBON_EXPLORER_INTERPRETER_BUILTINS_H_