type_structure.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. auto Print(llvm::raw_ostream& out) const -> void {
  42. out << "TypeStructure = ";
  43. for (auto s : structure_) {
  44. switch (s) {
  45. case Structural::Concrete:
  46. out << 'c';
  47. break;
  48. case Structural::Symbolic:
  49. out << '?';
  50. break;
  51. case Structural::ConcreteOpenParen:
  52. out << "(";
  53. break;
  54. case Structural::ConcreteCloseParen:
  55. out << ')';
  56. break;
  57. }
  58. }
  59. }
  60. private:
  61. friend class TypeStructureBuilder;
  62. enum class Structural : uint8_t {
  63. Concrete,
  64. ConcreteOpenParen,
  65. ConcreteCloseParen,
  66. Symbolic,
  67. };
  68. static constexpr int InfiniteDistance = -1;
  69. TypeStructure(llvm::SmallVector<Structural> structure,
  70. llvm::SmallVector<int> symbolic_type_indices)
  71. : structure_(std::move(structure)),
  72. symbolic_type_indices_(std::move(symbolic_type_indices)) {}
  73. // The structural position of concrete and symbolic values in the type.
  74. llvm::SmallVector<Structural> structure_;
  75. // Indices of the symbolic entries in structure_.
  76. llvm::SmallVector<int> symbolic_type_indices_;
  77. };
  78. // Constructs the TypeStructure for a self type or facet value and an interface
  79. // constraint (e.g. `Iface(A, B(C))`), which represents the location of unknown
  80. // symbolic values in the combined signature and which is ordered by them.
  81. //
  82. // Given `impl C as Z {}` the `self_const_id` would be a `C` and the interface
  83. // constraint would be `Z`.
  84. auto BuildTypeStructure(Context& context, SemIR::InstId self_inst_id,
  85. SemIR::SpecificInterface interface) -> TypeStructure;
  86. } // namespace Carbon::Check
  87. #endif // CARBON_TOOLCHAIN_CHECK_TYPE_STRUCTURE_H_