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