impl_scope.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #ifndef CARBON_EXPLORER_INTERPRETER_IMPL_SCOPE_H_
  5. #define CARBON_EXPLORER_INTERPRETER_IMPL_SCOPE_H_
  6. #include "explorer/ast/declaration.h"
  7. #include "explorer/interpreter/value.h"
  8. namespace Carbon {
  9. class TypeChecker;
  10. // The `ImplScope` class is responsible for mapping a type and
  11. // interface to the location of the witness table for the `impl` for
  12. // that type and interface. A scope may have parent scopes, whose
  13. // impls will also be visible in the child scope.
  14. //
  15. // There is typically one instance of `ImplScope` class per scope
  16. // because the impls that are visible for a given type and interface
  17. // can vary from scope to scope. For example, consider the `bar` and
  18. // `baz` methods in the following class C and nested class D.
  19. //
  20. // class C(U:! type, T:! type) {
  21. // class D(V:! type where U is Fooable(T)) {
  22. // fn bar[self: Self](x: U, y : T) -> T{
  23. // return x.foo(y)
  24. // }
  25. // }
  26. // fn baz[self: Self](x: U, y : T) -> T {
  27. // return x.foo(y);
  28. // }
  29. // }
  30. //
  31. // The call to `x.foo` in `bar` is valid because the `U is Fooable(T)`
  32. // impl is visible in the body of `bar`. In contrast, the call to
  33. // `x.foo` in `baz` is not valid because there is no visible impl for
  34. // `U` and `Fooable` in that scope.
  35. //
  36. // `ImplScope` also tracks the type equalities that are known in a particular
  37. // scope.
  38. class ImplScope {
  39. public:
  40. // Associates `iface` and `type` with the `impl` in this scope. If `iface` is
  41. // a constraint type, it will be split into its constituent components, and
  42. // any references to `.Self` are expected to have been substituted for the
  43. // type implementing the constraint.
  44. void Add(Nonnull<const Value*> iface, Nonnull<const Value*> type,
  45. Nonnull<const Witness*> witness, const TypeChecker& type_checker);
  46. // For a parameterized impl, associates `iface` and `type`
  47. // with the `impl` in this scope. Otherwise, the same as the previous
  48. // overload.
  49. void Add(Nonnull<const Value*> iface,
  50. llvm::ArrayRef<Nonnull<const GenericBinding*>> deduced,
  51. Nonnull<const Value*> type,
  52. llvm::ArrayRef<Nonnull<const ImplBinding*>> impl_bindings,
  53. Nonnull<const Witness*> witness, const TypeChecker& type_checker);
  54. // Adds a list of impl constraints from a constraint type into scope. Any
  55. // references to `.Self` are expected to have already been substituted for
  56. // the type implementing the constraint.
  57. void Add(llvm::ArrayRef<ImplConstraint> impls,
  58. llvm::ArrayRef<Nonnull<const GenericBinding*>> deduced,
  59. llvm::ArrayRef<Nonnull<const ImplBinding*>> impl_bindings,
  60. Nonnull<const Witness*> witness, const TypeChecker& type_checker);
  61. // Adds a type equality constraint.
  62. void AddEqualityConstraint(Nonnull<const EqualityConstraint*> equal) {
  63. equalities_.push_back(equal);
  64. }
  65. // Makes `parent` a parent of this scope.
  66. // REQUIRES: `parent` is not already a parent of this scope.
  67. void AddParent(Nonnull<const ImplScope*> parent);
  68. // Returns the associated impl for the given `constraint` and `type` in
  69. // the ancestor graph of this scope, or reports a compilation error
  70. // at `source_loc` there isn't exactly one matching impl.
  71. //
  72. // If any substitutions should be made into the constraint before resolving
  73. // it, those should be passed in `bindings`. The witness returned will be for
  74. // `constraint`, not for the result of substituting the bindings into the
  75. // constraint. The substituted type might in general have a different shape
  76. // of witness due to deduplication.
  77. auto Resolve(Nonnull<const Value*> constraint, Nonnull<const Value*> type,
  78. SourceLocation source_loc, const TypeChecker& type_checker,
  79. const Bindings& bindings = {}) const
  80. -> ErrorOr<Nonnull<const Witness*>>;
  81. // Same as Resolve, except that failure due to a missing implementation of a
  82. // constraint produces `nullopt` instead of an error if
  83. // `diagnose_missing_impl` is `false`. This is intended for cases where we're
  84. // selecting between options based on whether constraints are satisfied, such
  85. // as during `impl` selection.
  86. auto TryResolve(Nonnull<const Value*> constraint, Nonnull<const Value*> type,
  87. SourceLocation source_loc, const TypeChecker& type_checker,
  88. const Bindings& bindings, bool diagnose_missing_impl) const
  89. -> ErrorOr<std::optional<Nonnull<const Witness*>>>;
  90. // Visits the values that are a single step away from `value` according to an
  91. // equality constraint that is in scope. That is, the values `v` such that we
  92. // have a `value == v` equality constraint in scope.
  93. //
  94. // Stops and returns `false` if any call to the visitor returns `false`,
  95. // otherwise returns `true`.
  96. auto VisitEqualValues(
  97. Nonnull<const Value*> value,
  98. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool;
  99. void Print(llvm::raw_ostream& out) const;
  100. // The `Impl` struct is a key-value pair where the key is the
  101. // combination of a type and an interface, e.g., `List` and `Container`,
  102. // and the value is the result of statically resolving to the `impl`
  103. // for `List` as `Container`, which is an `Expression` that produces
  104. // the witness for that `impl`.
  105. // When the `impl` is parameterized, `deduced` and `impl_bindings`
  106. // are non-empty. The former contains the type parameters and the
  107. // later are impl bindings, that is, parameters for witnesses.
  108. struct Impl {
  109. Nonnull<const InterfaceType*> interface;
  110. std::vector<Nonnull<const GenericBinding*>> deduced;
  111. Nonnull<const Value*> type;
  112. std::vector<Nonnull<const ImplBinding*>> impl_bindings;
  113. Nonnull<const Witness*> witness;
  114. };
  115. private:
  116. // Returns the associated impl for the given `iface` and `type` in
  117. // the ancestor graph of this scope. Reports a compilation error
  118. // at `source_loc` if there's an ambiguity, or if `diagnose_missing_impl` is
  119. // set and there's no matching impl.
  120. auto TryResolveInterface(Nonnull<const InterfaceType*> iface,
  121. Nonnull<const Value*> type,
  122. SourceLocation source_loc,
  123. const TypeChecker& type_checker,
  124. bool diagnose_missing_impl) const
  125. -> ErrorOr<std::optional<Nonnull<const Witness*>>>;
  126. // Returns the associated impl for the given `iface` and `type` in
  127. // the ancestor graph of this scope, returns std::nullopt if there
  128. // is none, or reports a compilation error is there is not a most
  129. // specific impl for the given `iface` and `type`.
  130. // Use `original_scope` to satisfy requirements of any generic impl
  131. // that matches `iface` and `type`.
  132. auto TryResolveInterfaceRecursively(Nonnull<const InterfaceType*> iface_type,
  133. Nonnull<const Value*> type,
  134. SourceLocation source_loc,
  135. const ImplScope& original_scope,
  136. const TypeChecker& type_checker) const
  137. -> ErrorOr<std::optional<Nonnull<const Witness*>>>;
  138. // Returns the associated impl for the given `iface` and `type` in
  139. // this scope, returns std::nullopt if there is none, or reports
  140. // a compilation error is there is not a most specific impl for the
  141. // given `iface` and `type`.
  142. // Use `original_scope` to satisfy requirements of any generic impl
  143. // that matches `iface` and `type`.
  144. auto TryResolveInterfaceHere(Nonnull<const InterfaceType*> iface_type,
  145. Nonnull<const Value*> impl_type,
  146. SourceLocation source_loc,
  147. const ImplScope& original_scope,
  148. const TypeChecker& type_checker) const
  149. -> ErrorOr<std::optional<Nonnull<const Witness*>>>;
  150. std::vector<Impl> impls_;
  151. std::vector<Nonnull<const EqualityConstraint*>> equalities_;
  152. std::vector<Nonnull<const ImplScope*>> parent_scopes_;
  153. };
  154. // An equality context that considers two values to be equal if they are a
  155. // single step apart according to an equality constraint in the given impl
  156. // scope.
  157. struct SingleStepEqualityContext : public EqualityContext {
  158. public:
  159. explicit SingleStepEqualityContext(Nonnull<const ImplScope*> impl_scope)
  160. : impl_scope_(impl_scope) {}
  161. // Visits the values that are equal to the given value and a single step away
  162. // according to an equality constraint that is in the given impl scope. Stops
  163. // and returns `false` if the visitor returns `false`, otherwise returns
  164. // `true`.
  165. auto VisitEqualValues(Nonnull<const Value*> value,
  166. llvm::function_ref<bool(Nonnull<const Value*>)> visitor)
  167. const -> bool override;
  168. private:
  169. Nonnull<const ImplScope*> impl_scope_;
  170. };
  171. } // namespace Carbon
  172. #endif // CARBON_EXPLORER_INTERPRETER_IMPL_SCOPE_H_