impl_scope.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "executable_semantics/interpreter/impl_scope.h"
  5. #include "executable_semantics/common/error.h"
  6. #include "executable_semantics/interpreter/value.h"
  7. #include "llvm/Support/Casting.h"
  8. using llvm::cast;
  9. namespace Carbon {
  10. void ImplScope::Add(Nonnull<const Value*> iface, Nonnull<const Value*> type,
  11. ValueNodeView impl) {
  12. impls_.push_back({.interface = iface, .type = type, .impl = impl});
  13. }
  14. void ImplScope::AddParent(Nonnull<const ImplScope*> parent) {
  15. parent_scopes_.push_back(parent);
  16. }
  17. auto ImplScope::Resolve(Nonnull<const Value*> iface_type,
  18. Nonnull<const Value*> type,
  19. SourceLocation source_loc) const -> ValueNodeView {
  20. std::optional<ValueNodeView> result =
  21. TryResolve(iface_type, type, source_loc);
  22. if (!result.has_value()) {
  23. FATAL_COMPILATION_ERROR(source_loc) << "could not find implementation of "
  24. << *iface_type << " for " << *type;
  25. }
  26. return *result;
  27. }
  28. auto ImplScope::TryResolve(Nonnull<const Value*> iface_type,
  29. Nonnull<const Value*> type,
  30. SourceLocation source_loc) const
  31. -> std::optional<ValueNodeView> {
  32. std::optional<ValueNodeView> result =
  33. ResolveHere(iface_type, type, source_loc);
  34. if (result.has_value()) {
  35. return result;
  36. }
  37. for (Nonnull<const ImplScope*> parent : parent_scopes_) {
  38. auto parent_result = parent->TryResolve(iface_type, type, source_loc);
  39. if (parent_result.has_value() && result.has_value() &&
  40. *parent_result != *result) {
  41. FATAL_COMPILATION_ERROR(source_loc)
  42. << "ambiguous implementations of " << *iface_type << " for " << *type;
  43. }
  44. result = parent_result;
  45. }
  46. return result;
  47. }
  48. auto ImplScope::ResolveHere(Nonnull<const Value*> iface_type,
  49. Nonnull<const Value*> impl_type,
  50. SourceLocation source_loc) const
  51. -> std::optional<ValueNodeView> {
  52. switch (iface_type->kind()) {
  53. case Value::Kind::InterfaceType: {
  54. const auto& iface = cast<InterfaceType>(*iface_type);
  55. for (const Impl& impl : impls_) {
  56. if (TypeEqual(&iface, impl.interface) &&
  57. TypeEqual(impl_type, impl.type)) {
  58. return impl.impl;
  59. }
  60. }
  61. return std::nullopt;
  62. }
  63. default:
  64. FATAL() << "expected an interface, not " << *iface_type;
  65. break;
  66. }
  67. }
  68. } // namespace Carbon