facet_type.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_FACET_TYPE_H_
  5. #define CARBON_TOOLCHAIN_CHECK_FACET_TYPE_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Check {
  9. // Create a FacetType typed instruction object consisting of a interface. The
  10. // `specific_id` specifies arguments in the case the interface is generic.
  11. auto FacetTypeFromInterface(Context& context, SemIR::InterfaceId interface_id,
  12. SemIR::SpecificId specific_id) -> SemIR::FacetType;
  13. // Create a FacetType typed instruction object consisting of a named constraint.
  14. // The `specific_id` specifies arguments in the case the named constraint is
  15. // generic.
  16. auto FacetTypeFromNamedConstraint(Context& context,
  17. SemIR::NamedConstraintId named_constraint_id,
  18. SemIR::SpecificId specific_id)
  19. -> SemIR::FacetType;
  20. // Given an ImplWitnessAccessSubstituted, returns the InstId of the
  21. // ImplWitnessAccess. Otherwise, returns the input `inst_id` unchanged.
  22. //
  23. // This must be used when accessing the LHS of a rewrite constraint which has
  24. // not yet been resolved in order to preserve which associated constant is being
  25. // rewritten.
  26. auto GetImplWitnessAccessWithoutSubstitution(Context& context,
  27. SemIR::InstId inst_id)
  28. -> SemIR::InstId;
  29. // Perform rewrite constraint resolution for a facet type. The rewrite
  30. // constraints resolution is described here:
  31. // https://docs.carbon-lang.dev/docs/design/generics/appendix-rewrite-constraints.html#rewrite-constraint-resolution
  32. //
  33. // This function:
  34. // * Replaces the RHS of rewrite rules referring to `.Self` with the value
  35. // coming from other rewrite rules. For example in `.X = () and .Y = .X` the
  36. // result is `.X = () and .Y = ()`.
  37. // * Discards duplicate assignments to the same associated constant, such as in
  38. // `.X = () and .X = ()` which becomes just `.X = ()`.
  39. // * Diagnoses multiple assignments of different values to the same associated
  40. // constant such as `.X = () and .X = .Y`.
  41. // * Diagnoses cycles between rewrite rules such as `.X = .Y and .Y = .X` or
  42. // even `.X = .X`.
  43. //
  44. // The rewrite constraints in `rewrites` are modified in place and may be
  45. // reordered, with `ErrorInst` inserted when diagnosing errors.
  46. //
  47. // Returns false if resolve failed due to diagnosing an error. The resulting
  48. // value of the facet type should be an error constant.
  49. auto ResolveFacetTypeRewriteConstraints(
  50. Context& context, SemIR::LocId loc_id,
  51. llvm::SmallVector<SemIR::FacetTypeInfo::RewriteConstraint>& rewrites)
  52. -> bool;
  53. // Get a FacetType instruction for an empty FacetType. This is the facet
  54. // equivalent to TypeType.
  55. //
  56. // TODO: We vaguely plan to replace TypeType with this FacetType in the future,
  57. // though that's a big change.
  58. auto GetEmptyFacetType(Context& context) -> SemIR::TypeId;
  59. // Make a facet value for a type value, which has an empty FacetType as its
  60. // type. Returns a constant value, whose instruction payload is a FacetValue.
  61. auto GetConstantFacetValueForType(Context& context,
  62. SemIR::TypeInstId type_inst_id)
  63. -> SemIR::ConstantId;
  64. auto GetConstantFacetValueForTypeAndInterface(
  65. Context& context, SemIR::TypeInstId type_inst_id,
  66. SemIR::SpecificInterface specific_interface, SemIR::InstId witness_id)
  67. -> SemIR::ConstantId;
  68. } // namespace Carbon::Check
  69. #endif // CARBON_TOOLCHAIN_CHECK_FACET_TYPE_H_