facet_type.h 4.0 KB

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