facet_type.h 3.9 KB

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