require_impls.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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_REQUIRE_IMPLS_H_
  5. #define CARBON_TOOLCHAIN_CHECK_REQUIRE_IMPLS_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. #include "toolchain/sem_ir/require_impls.h"
  9. namespace Carbon::Check {
  10. // A type-safe wrapper around the specific id of a RequireImpls entity.
  11. // Constructed from a specific for its enclosing interface or constraint entity.
  12. struct RequireImplsSpecific {
  13. SemIR::SpecificId specific_id;
  14. };
  15. // Get the specific of a RequireImpls from the specific of its enclosing
  16. // interface or named constraint. Since a `require` declaration can not
  17. // introduce new generic bindings, the specific for the RequireImpls can be
  18. // constructed from the enclosing one.
  19. auto GetRequireImplsSpecificFromEnclosingSpecific(
  20. Context& context, const SemIR::RequireImpls& require,
  21. SemIR::SpecificId enclosing_specific_id) -> RequireImplsSpecific;
  22. // Like GetRequireImplsSpecificFromEnclosingSpecific but the `Self` value in the
  23. // specific is replaced by a facet value.
  24. //
  25. // The self facet value must have a facet type to match the type of `Self` in
  26. // specific, which will be a facet type for the enclosing interface. The self
  27. // type comes from `self_id`, and the `witness_id` must be an impl witness for
  28. // the enclosing interface.
  29. //
  30. // This is only possible for an enclosing interface since we do not ever have an
  31. // impl witness for a named constraint.
  32. auto GetRequireImplsSpecificFromEnclosingSpecificWithSelfType(
  33. Context& context, const SemIR::RequireImpls& require,
  34. SemIR::SpecificId enclosing_specific_id, SemIR::TypeInstId self_id,
  35. SemIR::InstId witness_id) -> RequireImplsSpecific;
  36. // Returns the constant value of `inst_id` from inside a RequireImpls
  37. // declaration, mapped into `enclosing_specific_id`. If an error results, it
  38. // returns ErrorInst.
  39. //
  40. // An error can occur during monomorphization when the self-type was valid as a
  41. // symbolic but becomes invalid with a more concrete specific.
  42. //
  43. // RequireImpls is always generic, so the instructions inside it must have a
  44. // specific applied. That specific is constructed from a specific for the
  45. // enclosing generic entity, with GetRequireImplsSpecificFromEnclosingSpecific.
  46. auto GetConstantValueInRequireImplsSpecific(Context& context,
  47. RequireImplsSpecific specific,
  48. SemIR::InstId inst_id)
  49. -> SemIR::ConstantId;
  50. } // namespace Carbon::Check
  51. #endif // CARBON_TOOLCHAIN_CHECK_REQUIRE_IMPLS_H_