enum-flags.h 14 KB

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