builtins.cpp 997 B

1234567891011121314151617181920212223242526272829303132
  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. }
  14. }
  15. auto Builtins::Get(SourceLocation source_loc, Builtin builtin) const
  16. -> ErrorOr<Nonnull<const Declaration*>> {
  17. std::optional<const Declaration*> result =
  18. builtins_[static_cast<int>(builtin)];
  19. if (!result.has_value()) {
  20. return CompilationError(source_loc)
  21. << "missing declaration for builtin `" << GetName(builtin) << "`";
  22. }
  23. return result.value();
  24. }
  25. } // namespace Carbon