impl_scope.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // Visits the values that are a single step away from `value` according to an
  82. // equality constraint that is in scope. That is, the values `v` such that we
  83. // have a `value == v` equality constraint in scope.
  84. //
  85. // Stops and returns `false` if any call to the visitor returns `false`,
  86. // otherwise returns `true`.
  87. auto VisitEqualValues(
  88. Nonnull<const Value*> value,
  89. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool;
  90. void Print(llvm::raw_ostream& out) const;
  91. // The `Impl` struct is a key-value pair where the key is the
  92. // combination of a type and an interface, e.g., `List` and `Container`,
  93. // and the value is the result of statically resolving to the `impl`
  94. // for `List` as `Container`, which is an `Expression` that produces
  95. // the witness for that `impl`.
  96. // When the `impl` is parameterized, `deduced` and `impl_bindings`
  97. // are non-empty. The former contains the type parameters and the
  98. // later are impl bindings, that is, parameters for witnesses.
  99. struct Impl {
  100. Nonnull<const InterfaceType*> interface;
  101. std::vector<Nonnull<const GenericBinding*>> deduced;
  102. Nonnull<const Value*> type;
  103. std::vector<Nonnull<const ImplBinding*>> impl_bindings;
  104. Nonnull<const Witness*> witness;
  105. };
  106. private:
  107. // Returns the associated impl for the given `iface` and `type` in
  108. // the ancestor graph of this scope, or reports a compilation error
  109. // at `source_loc` there isn't exactly one matching impl.
  110. auto ResolveInterface(Nonnull<const InterfaceType*> iface,
  111. Nonnull<const Value*> type, SourceLocation source_loc,
  112. const TypeChecker& type_checker) const
  113. -> ErrorOr<Nonnull<const Witness*>>;
  114. // Returns the associated impl for the given `iface` and `type` in
  115. // the ancestor graph of this scope, returns std::nullopt if there
  116. // is none, or reports a compilation error is there is not a most
  117. // specific impl for the given `iface` and `type`.
  118. // Use `original_scope` to satisfy requirements of any generic impl
  119. // that matches `iface` and `type`.
  120. auto TryResolve(Nonnull<const InterfaceType*> iface_type,
  121. Nonnull<const Value*> type, SourceLocation source_loc,
  122. const ImplScope& original_scope,
  123. const TypeChecker& type_checker) const
  124. -> ErrorOr<std::optional<Nonnull<const Witness*>>>;
  125. // Returns the associated impl for the given `iface` and `type` in
  126. // this scope, returns std::nullopt if there is none, or reports
  127. // a compilation error is there is not a most specific impl for the
  128. // given `iface` and `type`.
  129. // Use `original_scope` to satisfy requirements of any generic impl
  130. // that matches `iface` and `type`.
  131. auto ResolveHere(Nonnull<const InterfaceType*> iface_type,
  132. Nonnull<const Value*> impl_type, SourceLocation source_loc,
  133. const ImplScope& original_scope,
  134. const TypeChecker& type_checker) const
  135. -> ErrorOr<std::optional<Nonnull<const Witness*>>>;
  136. std::vector<Impl> impls_;
  137. std::vector<Nonnull<const EqualityConstraint*>> equalities_;
  138. std::vector<Nonnull<const ImplScope*>> parent_scopes_;
  139. };
  140. // An equality context that considers two values to be equal if they are a
  141. // single step apart according to an equality constraint in the given impl
  142. // scope.
  143. struct SingleStepEqualityContext : public EqualityContext {
  144. public:
  145. explicit SingleStepEqualityContext(Nonnull<const ImplScope*> impl_scope)
  146. : impl_scope_(impl_scope) {}
  147. // Visits the values that are equal to the given value and a single step away
  148. // according to an equality constraint that is in the given impl scope. Stops
  149. // and returns `false` if the visitor returns `false`, otherwise returns
  150. // `true`.
  151. auto VisitEqualValues(Nonnull<const Value*> value,
  152. llvm::function_ref<bool(Nonnull<const Value*>)> visitor)
  153. const -> bool override;
  154. private:
  155. Nonnull<const ImplScope*> impl_scope_;
  156. };
  157. } // namespace Carbon
  158. #endif // CARBON_EXPLORER_INTERPRETER_IMPL_SCOPE_H_