impl_scope.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/ADT/StringExtras.h"
  8. #include "llvm/Support/Casting.h"
  9. using llvm::cast;
  10. namespace Carbon {
  11. void ImplScope::Add(Nonnull<const Value*> iface, Nonnull<const Value*> type,
  12. ValueNodeView impl) {
  13. impls_.push_back({.interface = iface, .type = type, .impl = impl});
  14. }
  15. void ImplScope::AddParent(Nonnull<const ImplScope*> parent) {
  16. parent_scopes_.push_back(parent);
  17. }
  18. auto ImplScope::Resolve(Nonnull<const Value*> iface_type,
  19. Nonnull<const Value*> type,
  20. SourceLocation source_loc) const
  21. -> ErrorOr<ValueNodeView> {
  22. ASSIGN_OR_RETURN(std::optional<ValueNodeView> result,
  23. TryResolve(iface_type, type, source_loc));
  24. if (!result.has_value()) {
  25. return FATAL_COMPILATION_ERROR(source_loc)
  26. << "could not find implementation of " << *iface_type << " for "
  27. << *type;
  28. }
  29. return *result;
  30. }
  31. auto ImplScope::TryResolve(Nonnull<const Value*> iface_type,
  32. Nonnull<const Value*> type,
  33. SourceLocation source_loc) const
  34. -> ErrorOr<std::optional<ValueNodeView>> {
  35. std::optional<ValueNodeView> result =
  36. ResolveHere(iface_type, type, source_loc);
  37. if (result.has_value()) {
  38. return result;
  39. }
  40. for (Nonnull<const ImplScope*> parent : parent_scopes_) {
  41. ASSIGN_OR_RETURN(auto parent_result,
  42. parent->TryResolve(iface_type, type, source_loc));
  43. if (parent_result.has_value() && result.has_value() &&
  44. *parent_result != *result) {
  45. return FATAL_COMPILATION_ERROR(source_loc)
  46. << "ambiguous implementations of " << *iface_type << " for "
  47. << *type;
  48. }
  49. result = parent_result;
  50. }
  51. return result;
  52. }
  53. auto ImplScope::ResolveHere(Nonnull<const Value*> iface_type,
  54. Nonnull<const Value*> impl_type,
  55. SourceLocation /*source_loc*/) const
  56. -> std::optional<ValueNodeView> {
  57. switch (iface_type->kind()) {
  58. case Value::Kind::InterfaceType: {
  59. const auto& iface = cast<InterfaceType>(*iface_type);
  60. for (const Impl& impl : impls_) {
  61. if (TypeEqual(&iface, impl.interface) &&
  62. TypeEqual(impl_type, impl.type)) {
  63. return impl.impl;
  64. }
  65. }
  66. return std::nullopt;
  67. }
  68. default:
  69. FATAL() << "expected an interface, not " << *iface_type;
  70. break;
  71. }
  72. }
  73. // TODO: Add indentation when printing the parents.
  74. void ImplScope::Print(llvm::raw_ostream& out) const {
  75. out << "impls: ";
  76. llvm::ListSeparator sep;
  77. for (const Impl& impl : impls_) {
  78. out << sep << *(impl.type) << " as " << *(impl.interface);
  79. }
  80. out << "\n";
  81. for (const Nonnull<const ImplScope*>& parent : parent_scopes_) {
  82. out << *parent;
  83. }
  84. }
  85. } // namespace Carbon