facet_type.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Creates and returns an impl witness instruction for an impl declaration.
  35. //
  36. // If there are no rewrites into a name of the interface being implemented, a
  37. // placeholder witness table is created, to be replaced in the impl definition.
  38. //
  39. // Adds and returns an `ImplWitness` instruction (created with location set to
  40. // `witness_loc_id`) that shows "`Self` type" of type "facet type" (the value of
  41. // the `facet_type_inst_id` instruction) implements interface
  42. // `interface_to_witness`, which must be an interface required by "facet type"
  43. // (as determined by `RequireIdentifiedFacetType`). This witness reflects the
  44. // values assigned to associated constant members of that interface by rewrite
  45. // constraints in the facet type. `self_specific_id` will be the `specific_id`
  46. // of the resulting witness.
  47. //
  48. // `self_type_inst_id` is an instruction that evaluates to the `Self` type of
  49. // the facet type. For example, in `T:! X where ...`, we will bind the `.Self`
  50. // of the `where` facet type to `T`, and in `(X where ...) where ...`, we will
  51. // bind the inner `.Self` to the outer `.Self`.
  52. //
  53. // If the facet type contains a rewrite, we may have deferred converting the
  54. // rewritten value to the type of the associated constant. That conversion will
  55. // also be performed as part of resolution, and may depend on the `Self` type.
  56. auto InitialFacetTypeImplWitness(
  57. Context& context, SemIR::LocId witness_loc_id,
  58. SemIR::TypeInstId facet_type_inst_id, SemIR::TypeInstId self_type_inst_id,
  59. const SemIR::SpecificInterface& interface_to_witness,
  60. SemIR::SpecificId self_specific_id) -> SemIR::InstId;
  61. // Perform rewrite constraint resolution for a facet type. The rewrite
  62. // constraints resolution is described here:
  63. // https://docs.carbon-lang.dev/docs/design/generics/appendix-rewrite-constraints.html#rewrite-constraint-resolution
  64. //
  65. // This function:
  66. // * Replaces the RHS of rewrite rules referring to `.Self` with the value
  67. // coming from other rewrite rules. For example in `.X = () and .Y = .X` the
  68. // result is `.X = () and .Y = ()`.
  69. // * Discards duplicate assignments to the same associated constant, such as in
  70. // `.X = () and .X = ()` which becomes just `.X = ()`.
  71. // * Diagnoses multiple assignments of different values to the same associated
  72. // constant such as `.X = () and .X = .Y`.
  73. // * Diagnoses cycles between rewrite rules such as `.X = .Y and .Y = .X` or
  74. // even `.X = .X`.
  75. //
  76. // The rewrite constraints in `rewrites` are modified in place and may be
  77. // reordered, with `ErrorInst` inserted when diagnosing errors.
  78. //
  79. // Returns false if resolve failed due to diagnosing an error. The resulting
  80. // value of the facet type should be an error constant.
  81. auto ResolveFacetTypeRewriteConstraints(
  82. Context& context, SemIR::LocId loc_id,
  83. llvm::SmallVector<SemIR::FacetTypeInfo::RewriteConstraint>& rewrites)
  84. -> bool;
  85. // Introduce `.Self` as a symbolic binding into the current scope, and return
  86. // the `SymbolicBinding` instruction.
  87. //
  88. // The `self_type_id` is either a facet type (as `FacetType`) or `type` (as
  89. // `TypeType`).
  90. auto MakePeriodSelfFacetValue(Context& context, SemIR::TypeId self_type_id)
  91. -> SemIR::InstId;
  92. } // namespace Carbon::Check
  93. #endif // CARBON_TOOLCHAIN_CHECK_FACET_TYPE_H_