impl_scope.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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[me: Self](x: U, y : T) -> T{
  23. // return x.foo(y)
  24. // }
  25. // }
  26. // fn baz[me: 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.
  41. void Add(Nonnull<const Value*> iface, Nonnull<const Value*> type,
  42. Nonnull<Expression*> impl, const TypeChecker& type_checker);
  43. // For a parameterized impl, associates `iface` and `type`
  44. // with the `impl` in this scope.
  45. void Add(Nonnull<const Value*> iface,
  46. llvm::ArrayRef<Nonnull<const GenericBinding*>> deduced,
  47. Nonnull<const Value*> type,
  48. llvm::ArrayRef<Nonnull<const ImplBinding*>> impl_bindings,
  49. Nonnull<Expression*> impl, const TypeChecker& type_checker);
  50. // Add a type equality constraint.
  51. void AddEqualityConstraint(Nonnull<const EqualityConstraint*> equal) {
  52. equalities_.push_back(equal);
  53. }
  54. // Make `parent` a parent of this scope.
  55. // REQUIRES: `parent` is not already a parent of this scope.
  56. void AddParent(Nonnull<const ImplScope*> parent);
  57. // Returns the associated impl for the given `constraint` and `type` in
  58. // the ancestor graph of this scope, or reports a compilation error
  59. // at `source_loc` there isn't exactly one matching impl.
  60. auto Resolve(Nonnull<const Value*> constraint, Nonnull<const Value*> type,
  61. SourceLocation source_loc, const TypeChecker& type_checker) const
  62. -> ErrorOr<Nonnull<Expression*>>;
  63. // Visits the values that are a single step away from `value` according to an
  64. // equality constraint that is in scope. That is, the values `v` such that we
  65. // have a `value == v` equality constraint in scope.
  66. //
  67. // Stops and returns `false` if any call to the visitor returns `false`,
  68. // otherwise returns `true`.
  69. auto VisitEqualValues(
  70. Nonnull<const Value*> value,
  71. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool;
  72. void Print(llvm::raw_ostream& out) const;
  73. // The `Impl` struct is a key-value pair where the key is the
  74. // combination of a type and an interface, e.g., `List` and `Container`,
  75. // and the value is the result of statically resolving to the `impl`
  76. // for `List` as `Container`, which is an `Expression` that produces
  77. // the witness for that `impl`.
  78. // When the `impl` is parameterized, `deduced` and `impl_bindings`
  79. // are non-empty. The former contains the type parameters and the
  80. // later are impl bindings, that is, parameters for witnesses.
  81. struct Impl {
  82. Nonnull<const InterfaceType*> interface;
  83. std::vector<Nonnull<const GenericBinding*>> deduced;
  84. Nonnull<const Value*> type;
  85. std::vector<Nonnull<const ImplBinding*>> impl_bindings;
  86. Nonnull<Expression*> impl;
  87. };
  88. private:
  89. // Returns the associated impl for the given `iface` and `type` in
  90. // the ancestor graph of this scope, or reports a compilation error
  91. // at `source_loc` there isn't exactly one matching impl.
  92. auto ResolveInterface(Nonnull<const InterfaceType*> iface,
  93. Nonnull<const Value*> type, SourceLocation source_loc,
  94. const TypeChecker& type_checker) const
  95. -> ErrorOr<Nonnull<Expression*>>;
  96. // Returns the associated impl for the given `iface` and `type` in
  97. // the ancestor graph of this scope, returns std::nullopt if there
  98. // is none, or reports a compilation error is there is not a most
  99. // specific impl for the given `iface` and `type`.
  100. // Use `original_scope` to satisfy requirements of any generic impl
  101. // that matches `iface` and `type`.
  102. auto TryResolve(Nonnull<const InterfaceType*> iface_type,
  103. Nonnull<const Value*> type, SourceLocation source_loc,
  104. const ImplScope& original_scope,
  105. const TypeChecker& type_checker) const
  106. -> ErrorOr<std::optional<Nonnull<Expression*>>>;
  107. // Returns the associated impl for the given `iface` and `type` in
  108. // this scope, returns std::nullopt if there is none, or reports
  109. // a compilation error is there is not a most specific impl for the
  110. // given `iface` and `type`.
  111. // Use `original_scope` to satisfy requirements of any generic impl
  112. // that matches `iface` and `type`.
  113. auto ResolveHere(Nonnull<const InterfaceType*> iface_type,
  114. Nonnull<const Value*> impl_type, SourceLocation source_loc,
  115. const ImplScope& original_scope,
  116. const TypeChecker& type_checker) const
  117. -> ErrorOr<std::optional<Nonnull<Expression*>>>;
  118. std::vector<Impl> impls_;
  119. std::vector<Nonnull<const EqualityConstraint*>> equalities_;
  120. std::vector<Nonnull<const ImplScope*>> parent_scopes_;
  121. };
  122. } // namespace Carbon
  123. #endif // CARBON_EXPLORER_INTERPRETER_IMPL_SCOPE_H_