builtins.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  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. #include "explorer/interpreter/builtins.h"
  5. #include "explorer/common/error_builders.h"
  6. using llvm::dyn_cast;
  7. namespace Carbon {
  8. void Builtins::Register(Nonnull<const Declaration*> decl) {
  9. if (auto* interface = dyn_cast<InterfaceDeclaration>(decl)) {
  10. if (interface->name() == GetName(Builtin::ImplicitAs)) {
  11. builtins_[static_cast<int>(Builtin::ImplicitAs)] = interface;
  12. }
  13. if (interface->name() == GetName(Builtin::As)) {
  14. builtins_[static_cast<int>(Builtin::As)] = interface;
  15. }
  16. }
  17. }
  18. auto Builtins::Get(SourceLocation source_loc, Builtin builtin) const
  19. -> ErrorOr<Nonnull<const Declaration*>> {
  20. std::optional<const Declaration*> result =
  21. builtins_[static_cast<int>(builtin)];
  22. if (!result.has_value()) {
  23. return CompilationError(source_loc)
  24. << "missing declaration for builtin `" << GetName(builtin) << "`";
  25. }
  26. return result.value();
  27. }
  28. } // namespace Carbon