index_base.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_BASE_INDEX_BASE_H_
  5. #define CARBON_TOOLCHAIN_BASE_INDEX_BASE_H_
  6. #include <compare>
  7. #include <concepts>
  8. #include <iterator>
  9. #include <type_traits>
  10. #include "common/ostream.h"
  11. #include "llvm/ADT/iterator.h"
  12. namespace Carbon {
  13. template <typename DataType>
  14. class DataIterator;
  15. // Non-templated portions of `IdBase`.
  16. struct AnyIdBase {
  17. static constexpr int32_t NoneIndex = -1;
  18. AnyIdBase() = delete;
  19. constexpr explicit AnyIdBase(int index) : index(index) {}
  20. constexpr auto has_value() const -> bool { return index != NoneIndex; }
  21. int32_t index;
  22. };
  23. // A lightweight handle to an item identified by an opaque ID.
  24. //
  25. // This class is intended to be derived from by classes representing a specific
  26. // kind of ID, whose meaning as an integer is an implementation detail of the
  27. // type that vends the IDs. Typically this will be a vector index.
  28. //
  29. // Classes derived from IdBase are designed to be passed by value, not
  30. // reference or pointer. They are also designed to be small and efficient to
  31. // store in data structures.
  32. //
  33. // This uses CRTP for the `Print` function. Children should have:
  34. // static constexpr llvm::StringLiteral Label = "my_label";
  35. // Children can also define their own `Print` function, removing the dependency
  36. // on `Label`.
  37. template <typename IdT>
  38. struct IdBase : public AnyIdBase, public Printable<IdT> {
  39. using AnyIdBase::AnyIdBase;
  40. // Provide a standard `None` mapping to `NoneIndex`.
  41. //
  42. // This uses a `&` to trigger slightly different instantiation behaviors in
  43. // Clang. For context on why this is needed, see http://wg21.link/CWG2800.
  44. // NOLINTNEXTLINE(readability-identifier-naming)
  45. static const IdT& None;
  46. auto Print(llvm::raw_ostream& out) const -> void {
  47. out << IdT::Label;
  48. if (has_value()) {
  49. out << index;
  50. } else {
  51. out << "<none>";
  52. }
  53. }
  54. // Support simple equality comparison for ID types.
  55. friend constexpr auto operator==(IdBase<IdT> lhs, IdBase<IdT> rhs) -> bool {
  56. return lhs.index == rhs.index;
  57. }
  58. };
  59. template <typename IdT>
  60. constexpr const IdT& IdBase<IdT>::None = IdT(NoneIndex);
  61. // A lightweight handle to an item that behaves like an index.
  62. //
  63. // Unlike IdBase, classes derived from IndexBase are not completely opaque, and
  64. // provide at least an ordering between indexes that has meaning to an API
  65. // user. Additional semantics may be specified by the derived class.
  66. template <typename IdT>
  67. struct IndexBase : public IdBase<IdT> {
  68. using IdBase<IdT>::IdBase;
  69. // Support relational comparisons for index types.
  70. friend auto operator<=>(IndexBase<IdT> lhs, IndexBase<IdT> rhs)
  71. -> std::strong_ordering {
  72. return lhs.index <=> rhs.index;
  73. }
  74. };
  75. // A random-access iterator for arrays using IndexBase-derived types.
  76. template <typename IndexT>
  77. class IndexIterator
  78. : public llvm::iterator_facade_base<IndexIterator<IndexT>,
  79. std::random_access_iterator_tag,
  80. const IndexT, int>,
  81. public Printable<IndexIterator<IndexT>> {
  82. public:
  83. IndexIterator() = delete;
  84. explicit IndexIterator(IndexT index) : index_(index) {}
  85. friend auto operator==(const IndexIterator& lhs, const IndexIterator& rhs)
  86. -> bool {
  87. return lhs.index_ == rhs.index_;
  88. }
  89. friend auto operator<=>(const IndexIterator& lhs, const IndexIterator& rhs)
  90. -> std::strong_ordering {
  91. return lhs.index_ <=> rhs.index_;
  92. }
  93. auto operator*() const -> const IndexT& { return index_; }
  94. using llvm::iterator_facade_base<IndexIterator,
  95. std::random_access_iterator_tag,
  96. const IndexT, int>::operator-;
  97. friend auto operator-(const IndexIterator& lhs, const IndexIterator& rhs)
  98. -> int {
  99. return lhs.index_.index - rhs.index_.index;
  100. }
  101. auto operator+=(int n) -> IndexIterator& {
  102. index_.index += n;
  103. return *this;
  104. }
  105. auto operator-=(int n) -> IndexIterator& {
  106. index_.index -= n;
  107. return *this;
  108. }
  109. // Prints the raw token index.
  110. auto Print(llvm::raw_ostream& output) const -> void {
  111. output << index_.index;
  112. }
  113. private:
  114. IndexT index_;
  115. };
  116. } // namespace Carbon
  117. #endif // CARBON_TOOLCHAIN_BASE_INDEX_BASE_H_