custom_witness.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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_CUSTOM_WITNESS_H_
  5. #define CARBON_TOOLCHAIN_CHECK_CUSTOM_WITNESS_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Check {
  9. // Builds a witness that the given type implements the given interface,
  10. // populating it with the specified set of values. Returns a corresponding
  11. // lookup result. Produces a diagnostic and returns `None` if the specified
  12. // values aren't suitable for the interface.
  13. auto BuildCustomWitness(Context& context, SemIR::LocId loc_id,
  14. SemIR::ConstantId query_self_const_id,
  15. SemIR::SpecificInterfaceId query_specific_interface_id,
  16. llvm::ArrayRef<SemIR::InstId> values) -> SemIR::InstId;
  17. // Significant interfaces in `Core` which correspond to language features and
  18. // can have custom witnesses.
  19. enum class CoreInterface : std::int8_t {
  20. Copy = 1 << 0,
  21. Destroy = 1 << 1,
  22. IntFitsIn = 1 << 2,
  23. CppUnsafeDeref = 1 << 3,
  24. Unknown = -1,
  25. };
  26. // Given an interface, returns the corresponding enum if it's covered by
  27. // `CoreInterface`, or `Unknown` if it's some other interface.
  28. auto GetCoreInterface(Context& context, SemIR::InterfaceId interface_id)
  29. -> CoreInterface;
  30. // Returns a witness for a `CoreInterface` `CustomWitness`. A return value of
  31. // `None` indicates a non-final witness should be produced, while `std::nullopt`
  32. // indicates the query is final and no witness can be produced.
  33. auto LookupCustomWitness(Context& context, SemIR::LocId loc_id,
  34. CoreInterface core_interface,
  35. SemIR::ConstantId query_self_const_id,
  36. SemIR::SpecificInterfaceId query_specific_interface_id)
  37. -> std::optional<SemIR::InstId>;
  38. } // namespace Carbon::Check
  39. #endif // CARBON_TOOLCHAIN_CHECK_CUSTOM_WITNESS_H_