index_base.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. constexpr auto operator==(IdBase<IdT> rhs) const -> bool {
  50. return 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. auto operator<=>(IndexBase<IdT> rhs) const -> std::strong_ordering {
  63. return this->index <=> rhs.index;
  64. }
  65. };
  66. // A random-access iterator for arrays using IndexBase-derived types.
  67. template <typename IndexT>
  68. class IndexIterator
  69. : public llvm::iterator_facade_base<IndexIterator<IndexT>,
  70. std::random_access_iterator_tag,
  71. const IndexT, int>,
  72. public Printable<IndexIterator<IndexT>> {
  73. public:
  74. IndexIterator() = delete;
  75. explicit IndexIterator(IndexT index) : index_(index) {}
  76. auto operator==(const IndexIterator& rhs) const -> bool {
  77. return index_ == rhs.index_;
  78. }
  79. auto operator<=>(const IndexIterator& rhs) const -> std::strong_ordering {
  80. return index_ <=> rhs.index_;
  81. }
  82. auto operator*() const -> const IndexT& { return index_; }
  83. using llvm::iterator_facade_base<IndexIterator,
  84. std::random_access_iterator_tag,
  85. const IndexT, int>::operator-;
  86. auto operator-(const IndexIterator& rhs) const -> int {
  87. return index_.index - rhs.index_.index;
  88. }
  89. auto operator+=(int n) -> IndexIterator& {
  90. index_.index += n;
  91. return *this;
  92. }
  93. auto operator-=(int n) -> IndexIterator& {
  94. index_.index -= n;
  95. return *this;
  96. }
  97. // Prints the raw token index.
  98. auto Print(llvm::raw_ostream& output) const -> void {
  99. output << index_.index;
  100. }
  101. private:
  102. IndexT index_;
  103. };
  104. } // namespace Carbon
  105. #endif // CARBON_TOOLCHAIN_BASE_INDEX_BASE_H_