facet_type.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 a impl witness instruction for a facet type. The facet type is
  35. // required to be complete if `is_definition` is true or the facet type has
  36. // rewrites. Otherwise a placeholder witness is created, and
  37. // `AllocateFacetTypeImplWitness` can be used at 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
  55. // will also be performed as part of resolution, and may depend on the
  56. // `Self` type.
  57. auto InitialFacetTypeImplWitness(
  58. Context& context, SemIR::LocId witness_loc_id,
  59. SemIR::TypeInstId facet_type_inst_id, SemIR::TypeInstId self_type_inst_id,
  60. const SemIR::SpecificInterface& interface_to_witness,
  61. SemIR::SpecificId self_specific_id, bool is_definition) -> SemIR::InstId;
  62. // Returns `true` if the facet type is complete. Otherwise issues a diagnostic
  63. // and returns `false`.
  64. auto RequireCompleteFacetTypeForImplDefinition(
  65. Context& context, SemIR::LocId loc_id, SemIR::TypeInstId facet_type_inst_id)
  66. -> bool;
  67. // Replaces the placeholder created by `InitialFacetTypeImplWitness` with an
  68. // empty witness table of the right size. Requires the interface designated by
  69. // `interface_id` to be complete.
  70. auto AllocateFacetTypeImplWitness(Context& context,
  71. SemIR::InterfaceId interface_id,
  72. SemIR::InstBlockId witness_id) -> void;
  73. // Perform rewrite constraint resolution for a facet type. The rewrite
  74. // constraints resolution is described here:
  75. // https://docs.carbon-lang.dev/docs/design/generics/appendix-rewrite-constraints.html#rewrite-constraint-resolution
  76. //
  77. // This function:
  78. // * Replaces the RHS of rewrite rules referring to `.Self` with the value
  79. // coming from other rewrite rules. For example in `.X = () and .Y = .X` the
  80. // result is `.X = () and .Y = ()`.
  81. // * Discards duplicate assignments to the same associated constant, such as in
  82. // `.X = () and .X = ()` which becomes just `.X = ()`.
  83. // * Diagnoses multiple assignments of different values to the same associated
  84. // constant such as `.X = () and .X = .Y`.
  85. // * Diagnoses cycles between rewrite rules such as `.X = .Y and .Y = .X` or
  86. // even `.X = .X`.
  87. //
  88. // The rewrite constraints in `rewrites` are modified in place and may be
  89. // reordered, with `ErrorInst` inserted when diagnosing errors.
  90. //
  91. // Returns false if resolve failed due to diagnosing an error. The resulting
  92. // value of the facet type should be an error constant.
  93. auto ResolveFacetTypeRewriteConstraints(
  94. Context& context, SemIR::LocId loc_id,
  95. llvm::SmallVector<SemIR::FacetTypeInfo::RewriteConstraint>& rewrites)
  96. -> bool;
  97. // Introduce `.Self` as a symbolic binding into the current scope, and return
  98. // the `SymbolicBinding` instruction.
  99. //
  100. // The `self_type_id` is either a facet type (as `FacetType`) or `type` (as
  101. // `TypeType`).
  102. auto MakePeriodSelfFacetValue(Context& context, SemIR::TypeId self_type_id)
  103. -> SemIR::InstId;
  104. } // namespace Carbon::Check
  105. #endif // CARBON_TOOLCHAIN_CHECK_FACET_TYPE_H_