impl_scope.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/interpreter/type_checker.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. Nonnull<Expression*> impl) {
  13. Add(iface, {}, type, {}, impl);
  14. }
  15. void ImplScope::Add(Nonnull<const Value*> iface,
  16. llvm::ArrayRef<Nonnull<const GenericBinding*>> deduced,
  17. Nonnull<const Value*> type,
  18. llvm::ArrayRef<Nonnull<const ImplBinding*>> impl_bindings,
  19. Nonnull<Expression*> impl) {
  20. impls_.push_back({.interface = iface,
  21. .deduced = deduced,
  22. .type = type,
  23. .impl_bindings = impl_bindings,
  24. .impl = impl});
  25. }
  26. void ImplScope::AddParent(Nonnull<const ImplScope*> parent) {
  27. parent_scopes_.push_back(parent);
  28. }
  29. auto ImplScope::Resolve(Nonnull<const Value*> iface_type,
  30. Nonnull<const Value*> type, SourceLocation source_loc,
  31. const TypeChecker& type_checker) const
  32. -> ErrorOr<Nonnull<Expression*>> {
  33. ASSIGN_OR_RETURN(
  34. std::optional<Nonnull<Expression*>> result,
  35. TryResolve(iface_type, type, source_loc, *this, type_checker));
  36. if (!result.has_value()) {
  37. return CompilationError(source_loc) << "could not find implementation of "
  38. << *iface_type << " for " << *type;
  39. }
  40. return *result;
  41. }
  42. auto ImplScope::TryResolve(Nonnull<const Value*> iface_type,
  43. Nonnull<const Value*> type,
  44. SourceLocation source_loc,
  45. const ImplScope& original_scope,
  46. const TypeChecker& type_checker) const
  47. -> ErrorOr<std::optional<Nonnull<Expression*>>> {
  48. ASSIGN_OR_RETURN(
  49. std::optional<Nonnull<Expression*>> result,
  50. ResolveHere(iface_type, type, source_loc, original_scope, type_checker));
  51. for (Nonnull<const ImplScope*> parent : parent_scopes_) {
  52. ASSIGN_OR_RETURN(std::optional<Nonnull<Expression*>> parent_result,
  53. parent->TryResolve(iface_type, type, source_loc,
  54. original_scope, type_checker));
  55. if (parent_result.has_value()) {
  56. if (result.has_value()) {
  57. return CompilationError(source_loc) << "ambiguous implementations of "
  58. << *iface_type << " for " << *type;
  59. } else {
  60. result = *parent_result;
  61. }
  62. }
  63. }
  64. return result;
  65. }
  66. auto ImplScope::ResolveHere(Nonnull<const Value*> iface_type,
  67. Nonnull<const Value*> impl_type,
  68. SourceLocation source_loc,
  69. const ImplScope& original_scope,
  70. const TypeChecker& type_checker) const
  71. -> ErrorOr<std::optional<Nonnull<Expression*>>> {
  72. if (iface_type->kind() != Value::Kind::InterfaceType) {
  73. FATAL() << "expected an interface, not " << *iface_type;
  74. }
  75. const auto& iface = cast<InterfaceType>(*iface_type);
  76. std::optional<Nonnull<Expression*>> result = std::nullopt;
  77. for (const Impl& impl : impls_) {
  78. std::optional<Nonnull<Expression*>> m = type_checker.MatchImpl(
  79. iface, impl_type, impl, original_scope, source_loc);
  80. if (m.has_value()) {
  81. if (result.has_value()) {
  82. return CompilationError(source_loc)
  83. << "ambiguous implementations of " << *iface_type << " for "
  84. << *impl_type;
  85. } else {
  86. result = *m;
  87. }
  88. }
  89. }
  90. return result;
  91. }
  92. // TODO: Add indentation when printing the parents.
  93. void ImplScope::Print(llvm::raw_ostream& out) const {
  94. out << "impls: ";
  95. llvm::ListSeparator sep;
  96. for (const Impl& impl : impls_) {
  97. out << sep << *(impl.type) << " as " << *(impl.interface);
  98. }
  99. out << "\n";
  100. for (const Nonnull<const ImplScope*>& parent : parent_scopes_) {
  101. out << *parent;
  102. }
  103. }
  104. } // namespace Carbon