offset-type.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Offset types for GDB.
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Define an "offset" type. Offset types are distinct integer types
  15. that are used to represent an offset into anything that is
  16. addressable. For example, an offset into a DWARF debug section.
  17. The idea is catch mixing unrelated offset types at compile time, in
  18. code that needs to manipulate multiple different kinds of offsets
  19. that are easily confused. They're safer to use than native
  20. integers, because they have no implicit conversion to anything.
  21. And also, since they're implemented as "enum class" strong
  22. typedefs, they're still integers ABI-wise, making them a bit more
  23. efficient than wrapper structs on some ABIs.
  24. Some properties of offset types, loosely modeled on pointers:
  25. - You can compare offsets of the same type for equality and order.
  26. You can't compare an offset with an unrelated type.
  27. - You can add/substract an integer to/from an offset, which gives
  28. you back a shifted offset.
  29. - You can subtract two offsets of the same type, which gives you
  30. back the delta as an integer (of the enum class's underlying
  31. type), not as an offset type.
  32. - You can't add two offsets of the same type, as that would not
  33. make sense.
  34. However, unlike pointers, you can't deference offset types. */
  35. #ifndef COMMON_OFFSET_TYPE_H
  36. #define COMMON_OFFSET_TYPE_H
  37. /* Declare TYPE as being an offset type. This declares the type and
  38. enables the operators defined below. */
  39. #define DEFINE_OFFSET_TYPE(TYPE, UNDERLYING) \
  40. enum class TYPE : UNDERLYING {}; \
  41. void is_offset_type (TYPE)
  42. /* The macro macro is all you need to know use offset types. The rest
  43. below is all implementation detail. */
  44. /* For each enum class type that you want to support arithmetic
  45. operators, declare an "is_offset_type" overload that has exactly
  46. one parameter, of type that enum class. E.g.,:
  47. void is_offset_type (sect_offset);
  48. The function does not need to be defined, only declared.
  49. DEFINE_OFFSET_TYPE declares this.
  50. A function declaration is preferred over a traits type, because the
  51. former allows calling the DEFINE_OFFSET_TYPE macro inside a
  52. namespace to define the corresponding offset type in that
  53. namespace. The compiler finds the corresponding is_offset_type
  54. function via ADL.
  55. */
  56. /* Adding or subtracting an integer to an offset type shifts the
  57. offset. This is like "PTR = PTR + INT" and "PTR += INT". */
  58. #define DEFINE_OFFSET_ARITHM_OP(OP) \
  59. template<typename E, \
  60. typename = decltype (is_offset_type (std::declval<E> ()))> \
  61. constexpr E \
  62. operator OP (E lhs, typename std::underlying_type<E>::type rhs) \
  63. { \
  64. using underlying = typename std::underlying_type<E>::type; \
  65. return (E) (static_cast<underlying> (lhs) OP rhs); \
  66. } \
  67. \
  68. template<typename E, \
  69. typename = decltype (is_offset_type (std::declval<E> ()))> \
  70. constexpr E \
  71. operator OP (typename std::underlying_type<E>::type lhs, E rhs) \
  72. { \
  73. using underlying = typename std::underlying_type<E>::type; \
  74. return (E) (lhs OP static_cast<underlying> (rhs)); \
  75. } \
  76. \
  77. template<typename E, \
  78. typename = decltype (is_offset_type (std::declval<E> ()))> \
  79. E & \
  80. operator OP ## = (E &lhs, typename std::underlying_type<E>::type rhs) \
  81. { \
  82. using underlying = typename std::underlying_type<E>::type; \
  83. lhs = (E) (static_cast<underlying> (lhs) OP rhs); \
  84. return lhs; \
  85. }
  86. DEFINE_OFFSET_ARITHM_OP(+)
  87. DEFINE_OFFSET_ARITHM_OP(-)
  88. /* Adding two offset types doesn't make sense, just like "PTR + PTR"
  89. doesn't make sense. This is defined as a deleted function so that
  90. a compile error easily brings you to this comment. */
  91. template<typename E,
  92. typename = decltype (is_offset_type (std::declval<E> ()))>
  93. constexpr typename std::underlying_type<E>::type
  94. operator+ (E lhs, E rhs) = delete;
  95. /* Subtracting two offset types, however, gives you back the
  96. difference between the offsets, as an underlying type. Similar to
  97. how "PTR2 - PTR1" returns a ptrdiff_t. */
  98. template<typename E,
  99. typename = decltype (is_offset_type (std::declval<E> ()))>
  100. constexpr typename std::underlying_type<E>::type
  101. operator- (E lhs, E rhs)
  102. {
  103. using underlying = typename std::underlying_type<E>::type;
  104. return static_cast<underlying> (lhs) - static_cast<underlying> (rhs);
  105. }
  106. #endif /* COMMON_OFFSET_TYPE_H */