impl_lookup.h 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // Given a self facet, returns the canonical query self for a LookupImplWitness
  44. // instruction. The canonicalization looks through `FacetValue` and makes a
  45. // canonical form for `facet` and `facet as type`.
  46. //
  47. // If the input self facet is a `FacetValue` that is looked through, it can be
  48. // returned in `out_facet_value`, in order to preserve its facet type and
  49. // witnesses.
  50. auto GetCanonicalQuerySelfForLookupImplWitness(
  51. Context& context, SemIR::ConstantId self,
  52. SemIR::InstId* out_facet_value = nullptr) -> SemIR::ConstantId;
  53. // The kind of impl lookup being performed by a call to
  54. // `EvalLookupSingleFinalWitness`.
  55. enum class EvalImplLookupMode {
  56. // This is a regular impl lookup performed during check. If we produce a final
  57. // witness value that uses a specializable impl, the query will be poisoned so
  58. // that we will recheck it at the end of the compilation.
  59. Normal,
  60. // This is a re-check of a poisoned lookup being performed at the end of a
  61. // file. This disables any caching of lookup results for this query and redoes
  62. // the impl lookup.
  63. RecheckPoisonedLookup,
  64. };
  65. // Looks for a final witness for an impl lookup query consisting of a self (type
  66. // or facet) and a single interface. This is for eval to execute lookup via the
  67. // `LookupImplWitness` instruction. Since this query is re-evaluated against
  68. // specifics, it provides monomorphization of the impl lookup, which allows for
  69. // finding specializations.
  70. auto EvalLookupSingleFinalWitness(Context& context, SemIR::LocId loc_id,
  71. SemIR::LookupImplWitness eval_query,
  72. SemIR::InstId self_facet_value_inst_id,
  73. EvalImplLookupMode mode) -> SemIR::ConstantId;
  74. } // namespace Carbon::Check
  75. #endif // CARBON_TOOLCHAIN_CHECK_IMPL_LOOKUP_H_