btrace: Fix memory leak in btrace_clear.
[deliverable/binutils-gdb.git] / gdb / common / offset-type.h
CommitLineData
9c541725
PA
1/* Offset types for GDB.
2
3 Copyright (C) 2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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.
30
31 Some properties of offset types, loosely modeled on pointers:
32
33 - You can compare offsets of the same type for equality and order.
34 You can't compare an offset with an unrelated type.
35
36 - You can add/substract an integer to/from an offset, which gives
37 you back a shifted offset.
38
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.
42
43 - You can't add two offsets of the same type, as that would not
44 make sense.
45
46 However, unlike pointers, you can't deference offset types. */
47
48#ifndef COMMON_OFFSET_TYPE_H
49#define COMMON_OFFSET_TYPE_H
50
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)
56
57/* The macro macro is all you need to know use offset types. The rest
58 below is all implementation detail. */
59
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.,:
63
64 void is_offset_type (sect_offset);
65
66 The function does not need to be defined, only declared.
67 DEFINE_OFFSET_TYPE declares this.
68
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
73 function via ADL.
74*/
75
76#define DEFINE_OFFSET_REL_OP(OP) \
77 template<typename E, \
78 typename = decltype (is_offset_type (std::declval<E> ()))> \
79 constexpr bool \
80 operator OP (E lhs, E rhs) \
81 { \
82 using underlying = typename std::underlying_type<E>::type; \
83 return (static_cast<underlying> (lhs) \
84 OP static_cast<underlying> (lhs)); \
85 }
86
87DEFINE_OFFSET_REL_OP(>)
88DEFINE_OFFSET_REL_OP(>=)
89DEFINE_OFFSET_REL_OP(<)
90DEFINE_OFFSET_REL_OP(<=)
91
92/* Adding or subtracting an integer to an offset type shifts the
93 offset. This is like "PTR = PTR + INT" and "PTR += INT". */
94
95#define DEFINE_OFFSET_ARITHM_OP(OP) \
96 template<typename E, \
97 typename = decltype (is_offset_type (std::declval<E> ()))> \
98 constexpr E \
99 operator OP (E lhs, typename std::underlying_type<E>::type rhs) \
100 { \
101 using underlying = typename std::underlying_type<E>::type; \
102 return (E) (static_cast<underlying> (lhs) OP rhs); \
103 } \
104 \
105 template<typename E, \
106 typename = decltype (is_offset_type (std::declval<E> ()))> \
107 constexpr E \
108 operator OP (typename std::underlying_type<E>::type lhs, E rhs) \
109 { \
110 using underlying = typename std::underlying_type<E>::type; \
111 return (E) (lhs OP static_cast<underlying> (rhs)); \
112 } \
113 \
114 template<typename E, \
115 typename = decltype (is_offset_type (std::declval<E> ()))> \
116 E & \
117 operator OP ## = (E &lhs, typename std::underlying_type<E>::type rhs) \
118 { \
119 using underlying = typename std::underlying_type<E>::type; \
120 lhs = (E) (static_cast<underlying> (lhs) OP rhs); \
121 return lhs; \
122 }
123
124DEFINE_OFFSET_ARITHM_OP(+)
125DEFINE_OFFSET_ARITHM_OP(-)
126
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. */
130
131template<typename E,
132 typename = decltype (is_offset_type (std::declval<E> ()))>
133constexpr typename std::underlying_type<E>::type
134operator+ (E lhs, E rhs) = delete;
135
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. */
139
140template<typename E,
141 typename = decltype (is_offset_type (std::declval<E> ()))>
142constexpr typename std::underlying_type<E>::type
143operator- (E lhs, E rhs)
144{
145 using underlying = typename std::underlying_type<E>::type;
146 return static_cast<underlying> (lhs) - static_cast<underlying> (rhs);
147}
148
149#endif /* COMMON_OFFSET_TYPE_H */
This page took 0.038428 seconds and 4 git commands to generate.