type_structure.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_TYPE_STRUCTURE_H_
  5. #define CARBON_TOOLCHAIN_CHECK_TYPE_STRUCTURE_H_
  6. #include <algorithm>
  7. #include "common/ostream.h"
  8. #include "toolchain/check/context.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. #include "toolchain/sem_ir/impl.h"
  11. namespace Carbon::Check {
  12. // The "type structure" for an impl declaration.
  13. //
  14. // See
  15. // https://docs.carbon-lang.dev/docs/design/generics/overview.html#parameterized-impl-declarations.
  16. //
  17. // Type structures are ordered, and a type structure that is ordered higher is a
  18. // better, more specified, match.
  19. class TypeStructure : public Printable<TypeStructure> {
  20. public:
  21. // Returns whether the type structure is compatible with `other`. If false,
  22. // they can not possibly match with one being an `impl` for the other as a
  23. // lookup query.
  24. auto IsCompatibleWith(const TypeStructure& other) const -> bool;
  25. // Ordering of type structures. A lower value is a better match.
  26. // TODO: switch to operator<=> once we can depend on
  27. // std::lexicographical_compare_three_way (in particular, once we can
  28. // require clang-17 or newer, including in places like the GitHub test
  29. // runners).
  30. friend auto operator<(const TypeStructure& lhs, const TypeStructure& rhs)
  31. -> bool {
  32. return std::lexicographical_compare(
  33. lhs.symbolic_type_indices_.begin(), lhs.symbolic_type_indices_.end(),
  34. rhs.symbolic_type_indices_.begin(), rhs.symbolic_type_indices_.end(),
  35. [](int lhs_index, int rhs_index) {
  36. // A higher symbolic type index is a better match, so we need to
  37. // reverse the order.
  38. return rhs_index < lhs_index;
  39. });
  40. }
  41. // Equality of type structures. This compares that the structures are
  42. // identical, which is a stronger requirement than that they are ordered the
  43. // same.
  44. friend auto operator==(const TypeStructure& lhs, const TypeStructure& rhs)
  45. -> bool {
  46. return lhs.structure_ == rhs.structure_ &&
  47. lhs.concrete_types_ == rhs.concrete_types_;
  48. }
  49. auto Print(llvm::raw_ostream& out) const -> void {
  50. out << "TypeStructure = ";
  51. for (auto s : structure_) {
  52. switch (s) {
  53. case Structural::Concrete:
  54. out << 'c';
  55. break;
  56. case Structural::Symbolic:
  57. out << '?';
  58. break;
  59. case Structural::ConcreteOpenParen:
  60. out << "(";
  61. break;
  62. case Structural::ConcreteCloseParen:
  63. out << ')';
  64. break;
  65. }
  66. }
  67. }
  68. private:
  69. friend class TypeStructureBuilder;
  70. // Elements of the type structure, indicating the presence of a concrete or
  71. // symbolic element, and for aggregate concrete types (such as generic types),
  72. // nesting for the types inside.
  73. enum class Structural : uint8_t {
  74. // A concrete element in the type structure, such as `bool`.
  75. Concrete,
  76. // A concrete element in the type structure that contains nested types
  77. // within, such as `C(D)` for some classes C and D. It marks the start of
  78. // the nested and is paired with a ConcreteCloseParen at the end of the
  79. // nested types.
  80. ConcreteOpenParen,
  81. // Closes a ConcreteOpenParen for a concrete type with nested types.
  82. // Does not have its own concrete type.
  83. ConcreteCloseParen,
  84. // A symbolic element in the type structure. When matching type structures,
  85. // it represents a wildcard that matches against either a single `Concrete`
  86. // or `Symbolic`, or everything from a `ConcreteOpenParen` to its paired
  87. // `ConcreteCloseParen`.
  88. Symbolic,
  89. };
  90. // Indicates a concrete element in the type structure which does not add any
  91. // type information of its own. See `ConcreteType`.
  92. struct ConcreteNoneType {
  93. friend auto operator==(ConcreteNoneType /*lhs*/, ConcreteNoneType /*rhs*/)
  94. -> bool = default;
  95. };
  96. // The `concrete_types_` tracks the specific concrete type for each
  97. // `Structural::Concrete` or `Structural::ConcreteOpenParen` in the type
  98. // structure. But there are cases where the `ConcreteOpenParen` opens a scope
  99. // for other concrete types but doesn't add any type data of its own, and
  100. // `ConcreteNoneType` can appear there.
  101. using ConcreteType = std::variant<ConcreteNoneType, SemIR::TypeId,
  102. SemIR::ClassId, SemIR::InterfaceId>;
  103. TypeStructure(llvm::SmallVector<Structural> structure,
  104. llvm::SmallVector<int> symbolic_type_indices,
  105. llvm::SmallVector<ConcreteType> concrete_types)
  106. : structure_(std::move(structure)),
  107. symbolic_type_indices_(std::move(symbolic_type_indices)),
  108. concrete_types_(std::move(concrete_types)) {}
  109. // The structural position of concrete and symbolic values in the type.
  110. llvm::SmallVector<Structural> structure_;
  111. // Indices of the symbolic entries in structure_.
  112. llvm::SmallVector<int> symbolic_type_indices_;
  113. // The related value for each `Concrete` and `ConcreteOpenParen` entry in
  114. // the type `structure_`, in the same order. See `ConcreteType`.
  115. llvm::SmallVector<ConcreteType> concrete_types_;
  116. };
  117. // Constructs the TypeStructure for a self type or facet value and an interface
  118. // constraint (e.g. `Iface(A, B(C))`), which represents the location of unknown
  119. // symbolic values in the combined signature and which is ordered by them.
  120. //
  121. // Given `impl C as Z {}` the `self_const_id` would be a `C` and the interface
  122. // constraint would be `Z`.
  123. auto BuildTypeStructure(Context& context, SemIR::InstId self_inst_id,
  124. SemIR::SpecificInterface interface) -> TypeStructure;
  125. } // namespace Carbon::Check
  126. #endif // CARBON_TOOLCHAIN_CHECK_TYPE_STRUCTURE_H_