xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/enum-flags.h (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1 /* Copyright (C) 2015-2023 Free Software Foundation, Inc.
2 
3    This file is part of GDB.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #ifndef COMMON_ENUM_FLAGS_H
19 #define COMMON_ENUM_FLAGS_H
20 
21 #include "traits.h"
22 
23 /* Type-safe wrapper for enum flags.  enum flags are enums where the
24    values are bits that are meant to be ORed together.
25 
26    This allows writing code like the below, while with raw enums this
27    would fail to compile without casts to enum type at the assignments
28    to 'f':
29 
30     enum some_flag
31     {
32        flag_val1 = 1 << 1,
33        flag_val2 = 1 << 2,
34        flag_val3 = 1 << 3,
35        flag_val4 = 1 << 4,
36     };
37     DEF_ENUM_FLAGS_TYPE(enum some_flag, some_flags);
38 
39     some_flags f = flag_val1 | flag_val2;
40     f |= flag_val3;
41 
42    It's also possible to assign literal zero to an enum flags variable
43    (meaning, no flags), dispensing adding an awkward explicit "no
44    value" value to the enumeration.  For example:
45 
46     some_flags f = 0;
47     f |= flag_val3 | flag_val4;
48 
49    Note that literal integers other than zero fail to compile:
50 
51     some_flags f = 1; // error
52 */
53 
54 #ifdef __cplusplus
55 
56 /* Use this to mark an enum as flags enum.  It defines FLAGS_TYPE as
57    enum_flags wrapper class for ENUM, and enables the global operator
58    overloads for ENUM.  */
59 #define DEF_ENUM_FLAGS_TYPE(enum_type, flags_type)	\
60   typedef enum_flags<enum_type> flags_type;		\
61   void is_enum_flags_enum_type (enum_type *)
62 
63 /* To enable the global enum_flags operators for enum, declare an
64    "is_enum_flags_enum_type" overload that has exactly one parameter,
65    of type a pointer to that enum class.  E.g.,:
66 
67      void is_enum_flags_enum_type (enum some_flag *);
68 
69    The function does not need to be defined, only declared.
70    DEF_ENUM_FLAGS_TYPE declares this.
71 
72    A function declaration is preferred over a traits type, because the
73    former allows calling the DEF_ENUM_FLAGS_TYPE macro inside a
74    namespace to define the corresponding enum flags type in that
75    namespace.  The compiler finds the corresponding
76    is_enum_flags_enum_type function via ADL.  */
77 
78 /* Note that std::underlying_type<enum_type> is not what we want here,
79    since that returns unsigned int even when the enum decays to signed
80    int.  */
81 template<int size, bool sign> class integer_for_size { typedef void type; };
82 template<> struct integer_for_size<1, 0> { typedef uint8_t type; };
83 template<> struct integer_for_size<2, 0> { typedef uint16_t type; };
84 template<> struct integer_for_size<4, 0> { typedef uint32_t type; };
85 template<> struct integer_for_size<8, 0> { typedef uint64_t type; };
86 template<> struct integer_for_size<1, 1> { typedef int8_t type; };
87 template<> struct integer_for_size<2, 1> { typedef int16_t type; };
88 template<> struct integer_for_size<4, 1> { typedef int32_t type; };
89 template<> struct integer_for_size<8, 1> { typedef int64_t type; };
90 
91 template<typename T>
92 struct enum_underlying_type
93 {
94   DIAGNOSTIC_PUSH
95   DIAGNOSTIC_IGNORE_ENUM_CONSTEXPR_CONVERSION
96   typedef typename
97     integer_for_size<sizeof (T), static_cast<bool>(T (-1) < T (0))>::type
98     type;
99   DIAGNOSTIC_POP
100 };
101 
102 namespace enum_flags_detail
103 {
104 
105 /* Private type used to support initializing flag types with zero:
106 
107    foo_flags f = 0;
108 
109    but not other integers:
110 
111    foo_flags f = 1;
112 
113    The way this works is that we define an implicit constructor that
114    takes a pointer to this private type.  Since nothing can
115    instantiate an object of this type, the only possible pointer to
116    pass to the constructor is the NULL pointer, or, zero.  */
117 struct zero_type;
118 
119 /* gdb::Requires trait helpers.  */
120 template <typename enum_type>
121 using EnumIsUnsigned
122   = std::is_unsigned<typename enum_underlying_type<enum_type>::type>;
123 template <typename enum_type>
124 using EnumIsSigned
125   = std::is_signed<typename enum_underlying_type<enum_type>::type>;
126 
127 }
128 
129 template <typename E>
130 class enum_flags
131 {
132 public:
133   typedef E enum_type;
134   typedef typename enum_underlying_type<enum_type>::type underlying_type;
135 
136 public:
137   /* Allow default construction.  */
138   constexpr enum_flags ()
139     : m_enum_value ((enum_type) 0)
140   {}
141 
142   /* The default move/copy ctor/assignment do the right thing.  */
143 
144   /* If you get an error saying these two overloads are ambiguous,
145      then you tried to mix values of different enum types.  */
146   constexpr enum_flags (enum_type e)
147     : m_enum_value (e)
148   {}
149   constexpr enum_flags (enum_flags_detail::zero_type *zero)
150     : m_enum_value ((enum_type) 0)
151   {}
152 
153   enum_flags &operator&= (enum_flags e) &
154   {
155     m_enum_value = (enum_type) (m_enum_value & e.m_enum_value);
156     return *this;
157   }
158   enum_flags &operator|= (enum_flags e) &
159   {
160     m_enum_value = (enum_type) (m_enum_value | e.m_enum_value);
161     return *this;
162   }
163   enum_flags &operator^= (enum_flags e) &
164   {
165     m_enum_value = (enum_type) (m_enum_value ^ e.m_enum_value);
166     return *this;
167   }
168 
169   /* Delete rval versions.  */
170   void operator&= (enum_flags e) && = delete;
171   void operator|= (enum_flags e) && = delete;
172   void operator^= (enum_flags e) && = delete;
173 
174   /* Like raw enums, allow conversion to the underlying type.  */
175   constexpr operator underlying_type () const
176   {
177     return m_enum_value;
178   }
179 
180   /* Get the underlying value as a raw enum.  */
181   constexpr enum_type raw () const
182   {
183     return m_enum_value;
184   }
185 
186   /* Binary operations involving some unrelated type (which would be a
187      bug) are implemented as non-members, and deleted.  */
188 
189 private:
190   /* Stored as enum_type because GDB knows to print the bit flags
191      neatly if the enum values look like bit flags.  */
192   enum_type m_enum_value;
193 };
194 
195 template <typename E>
196 using is_enum_flags_enum_type_t
197   = decltype (is_enum_flags_enum_type (std::declval<E *> ()));
198 
199 /* Global operator overloads.  */
200 
201 /* Generate binary operators.  */
202 
203 #define ENUM_FLAGS_GEN_BINOP(OPERATOR_OP, OP)				\
204 									\
205   /* Raw enum on both LHS/RHS.  Returns raw enum type.  */		\
206   template <typename enum_type,						\
207 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
208   constexpr enum_type							\
209   OPERATOR_OP (enum_type e1, enum_type e2)				\
210   {									\
211     using underlying = typename enum_flags<enum_type>::underlying_type;	\
212     return (enum_type) (underlying (e1) OP underlying (e2));		\
213   }									\
214 									\
215   /* enum_flags on the LHS.  */						\
216   template <typename enum_type,						\
217 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
218   constexpr enum_flags<enum_type>					\
219   OPERATOR_OP (enum_flags<enum_type> e1, enum_type e2)			\
220   { return e1.raw () OP e2; }						\
221 									\
222   /* enum_flags on the RHS.  */						\
223   template <typename enum_type,						\
224 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
225   constexpr enum_flags<enum_type>					\
226   OPERATOR_OP (enum_type e1, enum_flags<enum_type> e2)			\
227   { return e1 OP e2.raw (); }						\
228 									\
229   /* enum_flags on both LHS/RHS.  */					\
230   template <typename enum_type,						\
231 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
232   constexpr enum_flags<enum_type>					\
233   OPERATOR_OP (enum_flags<enum_type> e1, enum_flags<enum_type> e2)	\
234   { return e1.raw () OP e2.raw (); }					\
235 									\
236   /* Delete cases involving unrelated types.  */			\
237 									\
238   template <typename enum_type, typename unrelated_type,		\
239 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
240   constexpr enum_flags<enum_type>					\
241   OPERATOR_OP (enum_type e1, unrelated_type e2) = delete;		\
242 									\
243   template <typename enum_type, typename unrelated_type,		\
244 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
245   constexpr enum_flags<enum_type>					\
246   OPERATOR_OP (unrelated_type e1, enum_type e2) = delete;		\
247 									\
248   template <typename enum_type, typename unrelated_type,		\
249 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
250   constexpr enum_flags<enum_type>					\
251   OPERATOR_OP (enum_flags<enum_type> e1, unrelated_type e2) = delete;	\
252 									\
253   template <typename enum_type, typename unrelated_type,		\
254 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
255   constexpr enum_flags<enum_type>					\
256   OPERATOR_OP (unrelated_type e1, enum_flags<enum_type> e2) = delete;
257 
258 /* Generate non-member compound assignment operators.  Only the raw
259    enum versions are defined here.  The enum_flags versions are
260    defined as member functions, simply because it's less code that
261    way.
262 
263    Note we delete operators that would allow e.g.,
264 
265      "enum_type | 1" or "enum_type1 | enum_type2"
266 
267    because that would allow a mistake like :
268      enum flags1 { F1_FLAGS1 = 1 };
269      enum flags2 { F2_FLAGS2 = 2 };
270      enum flags1 val;
271      switch (val) {
272        case F1_FLAGS1 | F2_FLAGS2:
273      ...
274 
275    If you really need to 'or' enumerators of different flag types,
276    cast to integer first.
277 */
278 #define ENUM_FLAGS_GEN_COMPOUND_ASSIGN(OPERATOR_OP, OP)			\
279   /* lval reference version.  */					\
280   template <typename enum_type,						\
281 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
282   constexpr enum_type &							\
283   OPERATOR_OP (enum_type &e1, enum_type e2)				\
284   { return e1 = e1 OP e2; }						\
285 									\
286   /* rval reference version.  */					\
287   template <typename enum_type,						\
288 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
289   void									\
290   OPERATOR_OP (enum_type &&e1, enum_type e2) = delete;			\
291 									\
292   /* Delete compound assignment from unrelated types.  */		\
293 									\
294   template <typename enum_type, typename other_enum_type,		\
295 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
296   constexpr enum_type &							\
297   OPERATOR_OP (enum_type &e1, other_enum_type e2) = delete;		\
298 									\
299   template <typename enum_type, typename other_enum_type,		\
300 	    typename = is_enum_flags_enum_type_t<enum_type>>		\
301   void									\
302   OPERATOR_OP (enum_type &&e1, other_enum_type e2) = delete;
303 
304 ENUM_FLAGS_GEN_BINOP (operator|, |)
305 ENUM_FLAGS_GEN_BINOP (operator&, &)
306 ENUM_FLAGS_GEN_BINOP (operator^, ^)
307 
308 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator|=, |)
309 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator&=, &)
310 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator^=, ^)
311 
312 /* Allow comparison with enum_flags, raw enum, and integers, only.
313    The latter case allows "== 0".  As side effect, it allows comparing
314    with integer variables too, but that's not a common mistake to
315    make.  It's important to disable comparison with unrelated types to
316    prevent accidentally comparing with unrelated enum values, which
317    are convertible to integer, and thus coupled with enum_flags
318    convertion to underlying type too, would trigger the built-in 'bool
319    operator==(unsigned, int)' operator.  */
320 
321 #define ENUM_FLAGS_GEN_COMP(OPERATOR_OP, OP)				\
322 									\
323   /* enum_flags OP enum_flags */					\
324 									\
325   template <typename enum_type>						\
326   constexpr bool							\
327   OPERATOR_OP (enum_flags<enum_type> lhs, enum_flags<enum_type> rhs)	\
328   { return lhs.raw () OP rhs.raw (); }					\
329 									\
330   /* enum_flags OP other */						\
331 									\
332   template <typename enum_type>						\
333   constexpr bool							\
334   OPERATOR_OP (enum_flags<enum_type> lhs, enum_type rhs)		\
335   { return lhs.raw () OP rhs; }						\
336 									\
337   template <typename enum_type>						\
338   constexpr bool							\
339   OPERATOR_OP (enum_flags<enum_type> lhs, int rhs)			\
340   { return lhs.raw () OP rhs; }						\
341 									\
342   template <typename enum_type, typename U>				\
343   constexpr bool							\
344   OPERATOR_OP (enum_flags<enum_type> lhs, U rhs) = delete;		\
345 									\
346   /* other OP enum_flags */						\
347 									\
348   template <typename enum_type>						\
349   constexpr bool							\
350   OPERATOR_OP (enum_type lhs, enum_flags<enum_type> rhs)		\
351   { return lhs OP rhs.raw (); }						\
352 									\
353   template <typename enum_type>						\
354   constexpr bool							\
355   OPERATOR_OP (int lhs, enum_flags<enum_type> rhs)			\
356   { return lhs OP rhs.raw (); }						\
357 									\
358   template <typename enum_type, typename U>				\
359   constexpr bool							\
360   OPERATOR_OP (U lhs, enum_flags<enum_type> rhs) = delete;
361 
362 ENUM_FLAGS_GEN_COMP (operator==, ==)
363 ENUM_FLAGS_GEN_COMP (operator!=, !=)
364 
365 /* Unary operators for the raw flags enum.  */
366 
367 /* We require underlying type to be unsigned when using operator~ --
368    if it were not unsigned, undefined behavior could result.  However,
369    asserting this in the class itself would require too many
370    unnecessary changes to usages of otherwise OK enum types.  */
371 template <typename enum_type,
372 	  typename = is_enum_flags_enum_type_t<enum_type>,
373 	  typename
374 	    = gdb::Requires<enum_flags_detail::EnumIsUnsigned<enum_type>>>
375 constexpr enum_type
376 operator~ (enum_type e)
377 {
378   using underlying = typename enum_flags<enum_type>::underlying_type;
379   return (enum_type) ~underlying (e);
380 }
381 
382 template <typename enum_type,
383 	  typename = is_enum_flags_enum_type_t<enum_type>,
384 	  typename = gdb::Requires<enum_flags_detail::EnumIsSigned<enum_type>>>
385 constexpr void operator~ (enum_type e) = delete;
386 
387 template <typename enum_type,
388 	  typename = is_enum_flags_enum_type_t<enum_type>,
389 	  typename
390 	    = gdb::Requires<enum_flags_detail::EnumIsUnsigned<enum_type>>>
391 constexpr enum_flags<enum_type>
392 operator~ (enum_flags<enum_type> e)
393 {
394   using underlying = typename enum_flags<enum_type>::underlying_type;
395   return (enum_type) ~underlying (e);
396 }
397 
398 template <typename enum_type,
399 	  typename = is_enum_flags_enum_type_t<enum_type>,
400 	  typename = gdb::Requires<enum_flags_detail::EnumIsSigned<enum_type>>>
401 constexpr void operator~ (enum_flags<enum_type> e) = delete;
402 
403 /* Delete operator<< and operator>>.  */
404 
405 template <typename enum_type, typename any_type,
406 	  typename = is_enum_flags_enum_type_t<enum_type>>
407 void operator<< (const enum_type &, const any_type &) = delete;
408 
409 template <typename enum_type, typename any_type,
410 	  typename = is_enum_flags_enum_type_t<enum_type>>
411 void operator<< (const enum_flags<enum_type> &, const any_type &) = delete;
412 
413 template <typename enum_type, typename any_type,
414 	  typename = is_enum_flags_enum_type_t<enum_type>>
415 void operator>> (const enum_type &, const any_type &) = delete;
416 
417 template <typename enum_type, typename any_type,
418 	  typename = is_enum_flags_enum_type_t<enum_type>>
419 void operator>> (const enum_flags<enum_type> &, const any_type &) = delete;
420 
421 #else /* __cplusplus */
422 
423 /* In C, the flags type is just a typedef for the enum type.  */
424 
425 #define DEF_ENUM_FLAGS_TYPE(enum_type, flags_type) \
426   typedef enum_type flags_type
427 
428 #endif /* __cplusplus */
429 
430 #endif /* COMMON_ENUM_FLAGS_H */
431