facet_type.h 4.3 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/ids.h"
  9. namespace Carbon::Check {
  10. // Create a FacetType typed instruction object consisting of a single
  11. // interface. The `specific_id` specifies arguments in the case the interface is
  12. // generic.
  13. auto FacetTypeFromInterface(Context& context, SemIR::InterfaceId interface_id,
  14. SemIR::SpecificId specific_id) -> SemIR::FacetType;
  15. // Creates a impl witness instruction for a facet type. The facet type is
  16. // required to be complete if `is_definition` is true or the facet type has
  17. // rewrites. Otherwise a placeholder witness is created, and
  18. // `AllocateFacetTypeImplWitness` can be used at the `impl` definition.
  19. //
  20. // Adds and returns an `ImplWitness` instruction (created with location set to
  21. // `witness_loc_id`) that shows "`Self` type" of type "facet type" (the value of
  22. // the `facet_type_inst_id` instruction) implements interface
  23. // `interface_to_witness`, which must be an interface required by "facet type"
  24. // (as determined by `RequireIdentifiedFacetType`). This witness reflects the
  25. // values assigned to associated constant members of that interface by rewrite
  26. // constraints in the facet type. `self_specific_id` will be the `specific_id`
  27. // of the resulting witness.
  28. //
  29. // `self_type_inst_id` is an instruction that evaluates to the `Self` type of
  30. // the facet type. For example, in `T:! X where ...`, we will bind the `.Self`
  31. // of the `where` facet type to `T`, and in `(X where ...) where ...`, we will
  32. // bind the inner `.Self` to the outer `.Self`.
  33. //
  34. // If the facet type contains a rewrite, we may have deferred converting the
  35. // rewritten value to the type of the associated constant. That conversion
  36. // will also be performed as part of resolution, and may depend on the
  37. // `Self` type.
  38. auto InitialFacetTypeImplWitness(
  39. Context& context, SemIR::LocId witness_loc_id,
  40. SemIR::TypeInstId facet_type_inst_id, SemIR::TypeInstId self_type_inst_id,
  41. const SemIR::SpecificInterface& interface_to_witness,
  42. SemIR::SpecificId self_specific_id, bool is_definition) -> SemIR::InstId;
  43. // Returns `true` if the facet type is complete. Otherwise issues a diagnostic
  44. // and returns `false`.
  45. auto RequireCompleteFacetTypeForImplDefinition(
  46. Context& context, SemIR::LocId loc_id, SemIR::TypeInstId facet_type_inst_id)
  47. -> bool;
  48. // Replaces the placeholder created by `InitialFacetTypeImplWitness` with an
  49. // empty witness table of the right size. Requires the interface designated by
  50. // `interface_id` to be complete.
  51. auto AllocateFacetTypeImplWitness(Context& context,
  52. SemIR::InterfaceId interface_id,
  53. SemIR::InstBlockId witness_id) -> void;
  54. // Perform rewrite constraint resolution for a facet type. The rewrite
  55. // constraints resolution is described here:
  56. // https://docs.carbon-lang.dev/docs/design/generics/appendix-rewrite-constraints.html#rewrite-constraint-resolution
  57. //
  58. // This function:
  59. // * Replaces the RHS of rewrite rules referring to `.Self` with the value
  60. // coming from other rewrite rules. For example in `.X = () and .Y = .X` the
  61. // result is `.X = () and .Y = ()`.
  62. // * Discards duplicate assignments to the same associated constant, such as in
  63. // `.X = () and .X = ()` which becomes just `.X = ()`.
  64. // * Diagnoses multiple assignments of different values to the same associated
  65. // constant such as `.X = () and .X = .Y`.
  66. // * Diagnoses cycles between rewrite rules such as `.X = .Y and .Y = .X` or
  67. // even `.X = .X`.
  68. //
  69. // The rewrite constraints in `rewrites` are modified in place and may be
  70. // reordered, with `ErrorInst` inserted when diagnosing errors.
  71. //
  72. // Returns false if resolve failed due to diagnosing an error. The resulting
  73. // value of the facet type should be an error constant.
  74. auto ResolveFacetTypeRewriteConstraints(
  75. Context& context, SemIR::LocId loc_id,
  76. llvm::SmallVector<SemIR::FacetTypeInfo::RewriteConstraint>& rewrites)
  77. -> bool;
  78. } // namespace Carbon::Check
  79. #endif // CARBON_TOOLCHAIN_CHECK_FACET_TYPE_H_