impl_lookup.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. bool diagnose = true) -> 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 kind of impl lookup being performed by a call to
  44. // `EvalLookupSingleFinalWitness`.
  45. enum class EvalImplLookupMode {
  46. // This is a regular impl lookup performed during check. If we produce a final
  47. // witness value that uses a specializable impl, the query will be poisoned so
  48. // that we will recheck it at the end of the compilation.
  49. Normal,
  50. // This is a re-check of a poisoned lookup being performed at the end of a
  51. // file. This disables any caching of lookup results for this query and redoes
  52. // the impl lookup.
  53. RecheckPoisonedLookup,
  54. };
  55. // Looks for a final witness for an impl lookup query consisting of a self (type
  56. // or facet) and a single interface. This is for eval to execute lookup via the
  57. // `LookupImplWitness` instruction. Since this query is re-evaluated against
  58. // specifics, it provides monomorphization of the impl lookup, which allows for
  59. // finding specializations.
  60. auto EvalLookupSingleFinalWitness(Context& context, SemIR::LocId loc_id,
  61. SemIR::LookupImplWitness eval_query,
  62. SemIR::InstId self_facet_value_inst_id,
  63. EvalImplLookupMode mode) -> SemIR::ConstantId;
  64. } // namespace Carbon::Check
  65. #endif // CARBON_TOOLCHAIN_CHECK_IMPL_LOOKUP_H_