impl_scope.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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(*constraint, witnesses,
  83. source_loc);
  84. }
  85. Bindings local_bindings = bindings;
  86. local_bindings.Add(constraint->self_binding(), impl_type, witness);
  87. CARBON_ASSIGN_OR_RETURN(
  88. Nonnull<const Witness*> result,
  89. ResolveInterface(cast<InterfaceType>(type_checker.Substitute(
  90. local_bindings, impl.interface)),
  91. type_checker.Substitute(local_bindings, impl.type),
  92. source_loc, type_checker));
  93. witnesses.push_back(result);
  94. }
  95. // Check that all equality constraints are satisfied in this scope.
  96. if (llvm::ArrayRef<EqualityConstraint> equals =
  97. constraint->equality_constraints();
  98. !equals.empty()) {
  99. std::optional<Nonnull<const Witness*>> witness;
  100. if (constraint->self_binding()->impl_binding()) {
  101. witness = type_checker.MakeConstraintWitness(*constraint, witnesses,
  102. source_loc);
  103. }
  104. Bindings local_bindings = bindings;
  105. local_bindings.Add(constraint->self_binding(), impl_type, witness);
  106. SingleStepEqualityContext equality_ctx(this);
  107. for (auto& equal : equals) {
  108. auto it = equal.values.begin();
  109. Nonnull<const Value*> first =
  110. type_checker.Substitute(local_bindings, *it++);
  111. for (; it != equal.values.end(); ++it) {
  112. Nonnull<const Value*> current =
  113. type_checker.Substitute(local_bindings, *it);
  114. if (!ValueEqual(first, current, &equality_ctx)) {
  115. return ProgramError(source_loc)
  116. << "constraint requires that " << *first
  117. << " == " << *current << ", which is not known to be true";
  118. }
  119. }
  120. }
  121. }
  122. return type_checker.MakeConstraintWitness(*constraint, std::move(witnesses),
  123. source_loc);
  124. }
  125. CARBON_FATAL() << "expected a constraint, not " << *constraint_type;
  126. }
  127. auto ImplScope::VisitEqualValues(
  128. Nonnull<const Value*> value,
  129. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool {
  130. for (Nonnull<const ConstraintType::EqualityConstraint*> eq : equalities_) {
  131. if (!eq->VisitEqualValues(value, visitor)) {
  132. return false;
  133. }
  134. }
  135. for (Nonnull<const ImplScope*> parent : parent_scopes_) {
  136. if (!parent->VisitEqualValues(value, visitor)) {
  137. return false;
  138. }
  139. }
  140. return true;
  141. }
  142. auto ImplScope::ResolveInterface(Nonnull<const InterfaceType*> iface_type,
  143. Nonnull<const Value*> type,
  144. SourceLocation source_loc,
  145. const TypeChecker& type_checker) const
  146. -> ErrorOr<Nonnull<const Witness*>> {
  147. CARBON_ASSIGN_OR_RETURN(
  148. std::optional<Nonnull<const Witness*>> result,
  149. TryResolve(iface_type, type, source_loc, *this, type_checker));
  150. if (!result.has_value()) {
  151. return ProgramError(source_loc) << "could not find implementation of "
  152. << *iface_type << " for " << *type;
  153. }
  154. return *result;
  155. }
  156. // Combines the results of two impl lookups. In the event of a tie, arbitrarily
  157. // prefer `a` over `b`.
  158. static auto CombineResults(Nonnull<const InterfaceType*> iface_type,
  159. Nonnull<const Value*> type,
  160. SourceLocation source_loc,
  161. std::optional<Nonnull<const Witness*>> a,
  162. std::optional<Nonnull<const Witness*>> b)
  163. -> ErrorOr<std::optional<Nonnull<const Witness*>>> {
  164. // If only one lookup succeeded, return that.
  165. if (!b) {
  166. return a;
  167. }
  168. if (!a) {
  169. return b;
  170. }
  171. // If either of them was a symbolic result, then they'll end up being
  172. // equivalent. In that case, pick `a`.
  173. auto* impl_a = dyn_cast<ImplWitness>(*a);
  174. auto* impl_b = dyn_cast<ImplWitness>(*b);
  175. if (!impl_b) {
  176. return a;
  177. }
  178. if (!impl_a) {
  179. return b;
  180. }
  181. // If they refer to the same `impl` declaration, it doesn't matter which one
  182. // we pick, so we pick `a`.
  183. // TODO: Compare the identities of the `impl`s, not the declarations.
  184. if (&impl_a->declaration() == &impl_b->declaration()) {
  185. return a;
  186. }
  187. // TODO: Order the `impl`s based on type structure.
  188. return ProgramError(source_loc)
  189. << "ambiguous implementations of " << *iface_type << " for " << *type;
  190. }
  191. auto ImplScope::TryResolve(Nonnull<const InterfaceType*> iface_type,
  192. Nonnull<const Value*> type,
  193. SourceLocation source_loc,
  194. const ImplScope& original_scope,
  195. const TypeChecker& type_checker) const
  196. -> ErrorOr<std::optional<Nonnull<const Witness*>>> {
  197. CARBON_ASSIGN_OR_RETURN(
  198. std::optional<Nonnull<const Witness*>> result,
  199. ResolveHere(iface_type, type, source_loc, original_scope, type_checker));
  200. for (Nonnull<const ImplScope*> parent : parent_scopes_) {
  201. CARBON_ASSIGN_OR_RETURN(
  202. std::optional<Nonnull<const Witness*>> parent_result,
  203. parent->TryResolve(iface_type, type, source_loc, original_scope,
  204. type_checker));
  205. CARBON_ASSIGN_OR_RETURN(result, CombineResults(iface_type, type, source_loc,
  206. result, parent_result));
  207. }
  208. return result;
  209. }
  210. auto ImplScope::ResolveHere(Nonnull<const InterfaceType*> iface_type,
  211. Nonnull<const Value*> impl_type,
  212. SourceLocation source_loc,
  213. const ImplScope& original_scope,
  214. const TypeChecker& type_checker) const
  215. -> ErrorOr<std::optional<Nonnull<const Witness*>>> {
  216. std::optional<Nonnull<const Witness*>> result = std::nullopt;
  217. for (const Impl& impl : impls_) {
  218. std::optional<Nonnull<const Witness*>> m = type_checker.MatchImpl(
  219. *iface_type, impl_type, impl, original_scope, source_loc);
  220. CARBON_ASSIGN_OR_RETURN(
  221. result, CombineResults(iface_type, impl_type, source_loc, result, m));
  222. }
  223. return result;
  224. }
  225. // TODO: Add indentation when printing the parents.
  226. void ImplScope::Print(llvm::raw_ostream& out) const {
  227. out << "impls: ";
  228. llvm::ListSeparator sep;
  229. for (const Impl& impl : impls_) {
  230. out << sep << *(impl.type) << " as " << *(impl.interface);
  231. }
  232. for (Nonnull<const ConstraintType::EqualityConstraint*> eq : equalities_) {
  233. out << sep;
  234. llvm::ListSeparator equal(" == ");
  235. for (Nonnull<const Value*> value : eq->values) {
  236. out << equal << *value;
  237. }
  238. }
  239. out << "\n";
  240. for (const Nonnull<const ImplScope*>& parent : parent_scopes_) {
  241. out << *parent;
  242. }
  243. }
  244. auto SingleStepEqualityContext::VisitEqualValues(
  245. Nonnull<const Value*> value,
  246. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool {
  247. return impl_scope_->VisitEqualValues(value, visitor);
  248. }
  249. } // namespace Carbon