index_base.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. auto Print(llvm::raw_ostream& out) const -> void {
  41. out << IdT::Label;
  42. if (has_value()) {
  43. out << index;
  44. } else {
  45. out << "<none>";
  46. }
  47. }
  48. // Support simple equality comparison for ID types.
  49. friend constexpr auto operator==(IdBase<IdT> lhs, IdBase<IdT> rhs) -> bool {
  50. return lhs.index == rhs.index;
  51. }
  52. };
  53. // A lightweight handle to an item that behaves like an index.
  54. //
  55. // Unlike IdBase, classes derived from IndexBase are not completely opaque, and
  56. // provide at least an ordering between indexes that has meaning to an API
  57. // user. Additional semantics may be specified by the derived class.
  58. template <typename IdT>
  59. struct IndexBase : public IdBase<IdT> {
  60. using IdBase<IdT>::IdBase;
  61. // Support relational comparisons for index types.
  62. friend auto operator<=>(IndexBase<IdT> lhs, IndexBase<IdT> rhs)
  63. -> std::strong_ordering {
  64. return lhs.index <=> rhs.index;
  65. }
  66. };
  67. // A random-access iterator for arrays using IndexBase-derived types.
  68. template <typename IndexT>
  69. class IndexIterator
  70. : public llvm::iterator_facade_base<IndexIterator<IndexT>,
  71. std::random_access_iterator_tag,
  72. const IndexT, int>,
  73. public Printable<IndexIterator<IndexT>> {
  74. public:
  75. IndexIterator() = delete;
  76. explicit IndexIterator(IndexT index) : index_(index) {}
  77. friend auto operator==(const IndexIterator& lhs, const IndexIterator& rhs)
  78. -> bool {
  79. return lhs.index_ == rhs.index_;
  80. }
  81. friend auto operator<=>(const IndexIterator& lhs, const IndexIterator& rhs)
  82. -> std::strong_ordering {
  83. return lhs.index_ <=> rhs.index_;
  84. }
  85. auto operator*() const -> const IndexT& { return index_; }
  86. using llvm::iterator_facade_base<IndexIterator,
  87. std::random_access_iterator_tag,
  88. const IndexT, int>::operator-;
  89. friend auto operator-(const IndexIterator& lhs, const IndexIterator& rhs)
  90. -> int {
  91. return lhs.index_.index - rhs.index_.index;
  92. }
  93. auto operator+=(int n) -> IndexIterator& {
  94. index_.index += n;
  95. return *this;
  96. }
  97. auto operator-=(int n) -> IndexIterator& {
  98. index_.index -= n;
  99. return *this;
  100. }
  101. // Prints the raw token index.
  102. auto Print(llvm::raw_ostream& output) const -> void {
  103. output << index_.index;
  104. }
  105. private:
  106. IndexT index_;
  107. };
  108. } // namespace Carbon
  109. #endif // CARBON_TOOLCHAIN_BASE_INDEX_BASE_H_