impl_lookup.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_TOOLCHAIN_CHECK_IMPL_LOOKUP_H_
  5. #define CARBON_TOOLCHAIN_CHECK_IMPL_LOOKUP_H_
  6. #include <variant>
  7. #include "toolchain/check/context.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. #include "toolchain/sem_ir/inst.h"
  10. #include "toolchain/sem_ir/typed_insts.h"
  11. namespace Carbon::Check {
  12. // Looks up the witnesses to use for a type value or facet value, and a facet
  13. // type naming a set of interfaces required to be implemented for that type, as
  14. // well as possible constraints on those interfaces.
  15. //
  16. // N.B. In the future, `TypeType` will become a facet type, at which point type
  17. // values will also be facet values.
  18. //
  19. // The return value is one of:
  20. // - An InstBlockId value, containing an `ImplWitness` instruction for each
  21. // required interface in the `query_facet_type_const_id`. This verifies the
  22. // facet type is satisfied for the type in `type_const_id`, and provides a
  23. // witness for accessing the impl of each interface.
  24. //
  25. // - `InstBlockId::None`, indicating lookup failed for at least one required
  26. // interface in the `query_facet_type_const_id`. The facet type is not
  27. // satisfied for the type in `type_const_id`. This represents lookup failure,
  28. // but is not an error, so no diagnostic is emitted.
  29. //
  30. // - An error value, indicating the program is invalid and a diagonstic has been
  31. // produced, either in this function or before.
  32. auto LookupImplWitness(Context& context, SemIR::LocId loc_id,
  33. SemIR::ConstantId query_self_const_id,
  34. SemIR::ConstantId query_facet_type_const_id)
  35. -> SemIR::InstBlockIdOrError;
  36. // Returns whether the query matches against the given impl. This is like a
  37. // `LookupImplWitness` operation but for a single interface, and against only
  38. // the single impl.
  39. auto LookupMatchesImpl(Context& context, SemIR::LocId loc_id,
  40. SemIR::ConstantId query_self_const_id,
  41. SemIR::SpecificInterface query_specific_interface,
  42. SemIR::ImplId target_impl) -> bool;
  43. // The result of EvalLookupSingleImplWitness(). It can be one of:
  44. // - No value. Lookup failed to find an impl declaration.
  45. // - An effectively final value. Lookup found either a concrete impl or a
  46. // `final` impl declaration; both can be used definitely. A witness is
  47. // available.
  48. // - A non-`final` symbolic value. Lookup found an impl, but it is not returned
  49. // since lookup will need to be done again with a more specific query to look
  50. // for specializations.
  51. class [[nodiscard]] EvalImplLookupResult {
  52. public:
  53. static auto MakeNone() -> EvalImplLookupResult {
  54. return EvalImplLookupResult(FoundNone());
  55. }
  56. static auto MakeFinal(SemIR::InstId inst_id) -> EvalImplLookupResult {
  57. return EvalImplLookupResult(inst_id);
  58. }
  59. static auto MakeNonFinal() -> EvalImplLookupResult {
  60. return EvalImplLookupResult(FoundNonFinalImpl());
  61. }
  62. // True if an impl declaration was found, either effectively final or
  63. // symbolic.
  64. auto has_value() const -> bool {
  65. return !std::holds_alternative<FoundNone>(value_);
  66. }
  67. // True if there is an effectively final witness in the result. If false, and
  68. // `has_value()` is true, it means an impl was found that's not effectively
  69. // final, and a further more specific query will need to be done.
  70. auto has_final_value() const -> bool {
  71. return std::holds_alternative<SemIR::InstId>(value_);
  72. }
  73. // Returns the witness id for an effectively final value's impl declaration.
  74. // Only valid to call if `has_final_value` is true.
  75. auto final_witness() const -> SemIR::InstId {
  76. return std::get<SemIR::InstId>(value_);
  77. }
  78. private:
  79. struct FoundNone {};
  80. struct FoundNonFinalImpl {};
  81. using Value = std::variant<SemIR::InstId, FoundNone, FoundNonFinalImpl>;
  82. explicit EvalImplLookupResult(Value value) : value_(value) {}
  83. Value value_;
  84. };
  85. // The kind of impl lookup being performed by a call to
  86. // `EvalLookupSingleImplWitness`.
  87. enum class EvalImplLookupMode {
  88. // This is a regular impl lookup performed during check. If we produce a final
  89. // witness value that uses a specializable impl, the query will be poisoned so
  90. // that we will recheck it at the end of the compilation.
  91. Normal,
  92. // This is a re-check of a poisoned lookup being performed at the end of a
  93. // file. This disables any caching of lookup results for this query and redoes
  94. // the impl lookup.
  95. RecheckPoisonedLookup,
  96. };
  97. // Looks for a witness instruction of an impl declaration for a query consisting
  98. // of a type value or facet value, and a single interface. This is for eval to
  99. // execute lookup via the LookupImplWitness instruction. It does not consider
  100. // the self facet value for finding a witness, since LookupImplWitness() would
  101. // have found that and not caused us to defer lookup to here.
  102. auto EvalLookupSingleImplWitness(Context& context, SemIR::LocId loc_id,
  103. SemIR::LookupImplWitness eval_query,
  104. SemIR::InstId self_facet_value_inst_id,
  105. EvalImplLookupMode mode)
  106. -> EvalImplLookupResult;
  107. } // namespace Carbon::Check
  108. #endif // CARBON_TOOLCHAIN_CHECK_IMPL_LOOKUP_H_