impl_scope.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/impl_scope.h"
  5. #include "explorer/interpreter/type_checker.h"
  6. #include "explorer/interpreter/value.h"
  7. #include "llvm/ADT/StringExtras.h"
  8. #include "llvm/Support/Casting.h"
  9. using llvm::cast;
  10. using llvm::dyn_cast;
  11. namespace Carbon {
  12. void ImplScope::Add(Nonnull<const Value*> iface, Nonnull<const Value*> type,
  13. Nonnull<const Witness*> witness,
  14. const TypeChecker& type_checker) {
  15. Add(iface, {}, type, {}, witness, type_checker);
  16. }
  17. void ImplScope::Add(Nonnull<const Value*> iface,
  18. llvm::ArrayRef<Nonnull<const GenericBinding*>> deduced,
  19. Nonnull<const Value*> type,
  20. llvm::ArrayRef<Nonnull<const ImplBinding*>> impl_bindings,
  21. Nonnull<const Witness*> witness,
  22. const TypeChecker& type_checker) {
  23. if (auto* constraint = dyn_cast<ConstraintType>(iface)) {
  24. // The caller should have substituted `.Self` for `type` already.
  25. Add(constraint->impl_constraints(), deduced, impl_bindings, witness,
  26. type_checker);
  27. // A parameterized impl declaration doesn't contribute any equality
  28. // constraints to the scope. Instead, we'll resolve the equality
  29. // constraints by resolving a witness when needed.
  30. if (deduced.empty()) {
  31. for (auto& equality_constraint : constraint->equality_constraints()) {
  32. equalities_.push_back(&equality_constraint);
  33. }
  34. }
  35. return;
  36. }
  37. impls_.push_back({.interface = cast<InterfaceType>(iface),
  38. .deduced = deduced,
  39. .type = type,
  40. .impl_bindings = impl_bindings,
  41. .witness = witness});
  42. }
  43. void ImplScope::Add(llvm::ArrayRef<ConstraintType::ImplConstraint> impls,
  44. llvm::ArrayRef<Nonnull<const GenericBinding*>> deduced,
  45. llvm::ArrayRef<Nonnull<const ImplBinding*>> impl_bindings,
  46. Nonnull<const Witness*> witness,
  47. const TypeChecker& type_checker) {
  48. for (size_t i = 0; i != impls.size(); ++i) {
  49. ConstraintType::ImplConstraint impl = impls[i];
  50. Add(impl.interface, deduced, impl.type, impl_bindings,
  51. type_checker.MakeConstraintWitnessAccess(witness, i), type_checker);
  52. }
  53. }
  54. void ImplScope::AddParent(Nonnull<const ImplScope*> parent) {
  55. parent_scopes_.push_back(parent);
  56. }
  57. auto ImplScope::Resolve(Nonnull<const Value*> constraint_type,
  58. Nonnull<const Value*> impl_type,
  59. SourceLocation source_loc,
  60. const TypeChecker& type_checker,
  61. const Bindings& bindings) const
  62. -> ErrorOr<Nonnull<const Witness*>> {
  63. if (const auto* iface_type = dyn_cast<InterfaceType>(constraint_type)) {
  64. iface_type =
  65. cast<InterfaceType>(type_checker.Substitute(bindings, iface_type));
  66. return ResolveInterface(iface_type, impl_type, source_loc, type_checker);
  67. }
  68. if (const auto* constraint = dyn_cast<ConstraintType>(constraint_type)) {
  69. std::vector<Nonnull<const Witness*>> witnesses;
  70. for (auto impl : constraint->impl_constraints()) {
  71. // Note that later impl constraints can refer to earlier impl constraints
  72. // via impl bindings. For example, in
  73. // `C where .Self.AssocType is D`,
  74. // ... the `.Self.AssocType is D` constraint refers to the `.Self is C`
  75. // constraint when naming `AssocType`. So incrementally build up a
  76. // partial constraint witness as we go.
  77. std::optional<Nonnull<const Witness*>> witness;
  78. if (constraint->self_binding()->impl_binding()) {
  79. // Note, this is a partial impl binding covering only the impl
  80. // constraints that we've already seen. Earlier impl constraints should
  81. // not be able to refer to impl bindings for later impl constraints.
  82. witness = type_checker.MakeConstraintWitness(witnesses);
  83. }
  84. Bindings local_bindings = bindings;
  85. local_bindings.Add(constraint->self_binding(), impl_type, witness);
  86. CARBON_ASSIGN_OR_RETURN(
  87. Nonnull<const Witness*> result,
  88. ResolveInterface(cast<InterfaceType>(type_checker.Substitute(
  89. local_bindings, impl.interface)),
  90. type_checker.Substitute(local_bindings, impl.type),
  91. source_loc, type_checker));
  92. witnesses.push_back(result);
  93. }
  94. // Check that all equality constraints are satisfied in this scope.
  95. if (llvm::ArrayRef<EqualityConstraint> equals =
  96. constraint->equality_constraints();
  97. !equals.empty()) {
  98. std::optional<Nonnull<const Witness*>> witness;
  99. if (constraint->self_binding()->impl_binding()) {
  100. witness = type_checker.MakeConstraintWitness(witnesses);
  101. }
  102. Bindings local_bindings = bindings;
  103. local_bindings.Add(constraint->self_binding(), impl_type, witness);
  104. SingleStepEqualityContext equality_ctx(this);
  105. for (auto& equal : equals) {
  106. auto it = equal.values.begin();
  107. Nonnull<const Value*> first =
  108. type_checker.Substitute(local_bindings, *it++);
  109. for (; it != equal.values.end(); ++it) {
  110. Nonnull<const Value*> current =
  111. type_checker.Substitute(local_bindings, *it);
  112. if (!ValueEqual(first, current, &equality_ctx)) {
  113. return ProgramError(source_loc)
  114. << "constraint requires that " << *first
  115. << " == " << *current << ", which is not known to be true";
  116. }
  117. }
  118. }
  119. }
  120. return type_checker.MakeConstraintWitness(std::move(witnesses));
  121. }
  122. CARBON_FATAL() << "expected a constraint, not " << *constraint_type;
  123. }
  124. auto ImplScope::VisitEqualValues(
  125. Nonnull<const Value*> value,
  126. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool {
  127. for (Nonnull<const ConstraintType::EqualityConstraint*> eq : equalities_) {
  128. if (!eq->VisitEqualValues(value, visitor)) {
  129. return false;
  130. }
  131. }
  132. for (Nonnull<const ImplScope*> parent : parent_scopes_) {
  133. if (!parent->VisitEqualValues(value, visitor)) {
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. auto ImplScope::ResolveInterface(Nonnull<const InterfaceType*> iface_type,
  140. Nonnull<const Value*> type,
  141. SourceLocation source_loc,
  142. const TypeChecker& type_checker) const
  143. -> ErrorOr<Nonnull<const Witness*>> {
  144. CARBON_ASSIGN_OR_RETURN(
  145. std::optional<Nonnull<const Witness*>> result,
  146. TryResolve(iface_type, type, source_loc, *this, type_checker));
  147. if (!result.has_value()) {
  148. return ProgramError(source_loc) << "could not find implementation of "
  149. << *iface_type << " for " << *type;
  150. }
  151. return *result;
  152. }
  153. // Combines the results of two impl lookups. In the event of a tie, arbitrarily
  154. // prefer `a` over `b`.
  155. static auto CombineResults(Nonnull<const InterfaceType*> iface_type,
  156. Nonnull<const Value*> type,
  157. SourceLocation source_loc,
  158. std::optional<Nonnull<const Witness*>> a,
  159. std::optional<Nonnull<const Witness*>> b)
  160. -> ErrorOr<std::optional<Nonnull<const Witness*>>> {
  161. // If only one lookup succeeded, return that.
  162. if (!b) {
  163. return a;
  164. }
  165. if (!a) {
  166. return b;
  167. }
  168. // If either of them was a symbolic result, then they'll end up being
  169. // equivalent. In that case, pick `a`.
  170. auto* impl_a = dyn_cast<ImplWitness>(*a);
  171. auto* impl_b = dyn_cast<ImplWitness>(*b);
  172. if (!impl_b) {
  173. return a;
  174. }
  175. if (!impl_a) {
  176. return b;
  177. }
  178. // If they refer to the same `impl` declaration, it doesn't matter which one
  179. // we pick, so we pick `a`.
  180. // TODO: Compare the identities of the `impl`s, not the declarations.
  181. if (&impl_a->declaration() == &impl_b->declaration()) {
  182. return a;
  183. }
  184. // TODO: Order the `impl`s based on type structure.
  185. return ProgramError(source_loc)
  186. << "ambiguous implementations of " << *iface_type << " for " << *type;
  187. }
  188. auto ImplScope::TryResolve(Nonnull<const InterfaceType*> iface_type,
  189. Nonnull<const Value*> type,
  190. SourceLocation source_loc,
  191. const ImplScope& original_scope,
  192. const TypeChecker& type_checker) const
  193. -> ErrorOr<std::optional<Nonnull<const Witness*>>> {
  194. CARBON_ASSIGN_OR_RETURN(
  195. std::optional<Nonnull<const Witness*>> result,
  196. ResolveHere(iface_type, type, source_loc, original_scope, type_checker));
  197. for (Nonnull<const ImplScope*> parent : parent_scopes_) {
  198. CARBON_ASSIGN_OR_RETURN(
  199. std::optional<Nonnull<const Witness*>> parent_result,
  200. parent->TryResolve(iface_type, type, source_loc, original_scope,
  201. type_checker));
  202. CARBON_ASSIGN_OR_RETURN(result, CombineResults(iface_type, type, source_loc,
  203. result, parent_result));
  204. }
  205. return result;
  206. }
  207. auto ImplScope::ResolveHere(Nonnull<const InterfaceType*> iface_type,
  208. Nonnull<const Value*> impl_type,
  209. SourceLocation source_loc,
  210. const ImplScope& original_scope,
  211. const TypeChecker& type_checker) const
  212. -> ErrorOr<std::optional<Nonnull<const Witness*>>> {
  213. std::optional<Nonnull<const Witness*>> result = std::nullopt;
  214. for (const Impl& impl : impls_) {
  215. std::optional<Nonnull<const Witness*>> m = type_checker.MatchImpl(
  216. *iface_type, impl_type, impl, original_scope, source_loc);
  217. CARBON_ASSIGN_OR_RETURN(
  218. result, CombineResults(iface_type, impl_type, source_loc, result, m));
  219. }
  220. return result;
  221. }
  222. // TODO: Add indentation when printing the parents.
  223. void ImplScope::Print(llvm::raw_ostream& out) const {
  224. out << "impls: ";
  225. llvm::ListSeparator sep;
  226. for (const Impl& impl : impls_) {
  227. out << sep << *(impl.type) << " as " << *(impl.interface);
  228. }
  229. for (Nonnull<const ConstraintType::EqualityConstraint*> eq : equalities_) {
  230. out << sep;
  231. llvm::ListSeparator equal(" == ");
  232. for (Nonnull<const Value*> value : eq->values) {
  233. out << equal << *value;
  234. }
  235. }
  236. out << "\n";
  237. for (const Nonnull<const ImplScope*>& parent : parent_scopes_) {
  238. out << *parent;
  239. }
  240. }
  241. auto SingleStepEqualityContext::VisitEqualValues(
  242. Nonnull<const Value*> value,
  243. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool {
  244. return impl_scope_->VisitEqualValues(value, visitor);
  245. }
  246. } // namespace Carbon