impl_scope.cpp 11 KB

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