impl_scope.cpp 3.1 KB

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