llvm.org GIT mirror llvm / 972586b
[ADT] Add LLVM_MARK_AS_BITMASK_ENUM, used to enable bitwise operations on enums without static_cast. Summary: Normally when you do a bitwise operation on an enum value, you get back an instance of the underlying type (e.g. int). But using this macro, bitwise ops on your enum will return you back instances of the enum. This is particularly useful for enums which represent a combination of flags. Suppose you have a function which takes an int and a set of flags. One way to do this would be to take two numeric params: enum SomeFlags { F1 = 1, F2 = 2, F3 = 4, ... }; void Fn(int Num, int Flags); void foo() { Fn(42, F2 | F3); } But now if you get the order of arguments wrong, you won't get an error. You might try to fix this by changing the signature of Fn so it accepts a SomeFlags arg: enum SomeFlags { F1 = 1, F2 = 2, F3 = 4, ... }; void Fn(int Num, SomeFlags Flags); void foo() { Fn(42, static_cast<SomeFlags>(F2 | F3)); } But now we need a static cast after doing "F2 | F3" because the result of that computation is the enum's underlying type. This patch adds a mechanism which gives us the safety of the second approach with the brevity of the first. enum SomeFlags { F1 = 1, F2 = 2, F3 = 4, ..., F_MAX = 128, LLVM_MARK_AS_BITMASK_ENUM(F_MAX) }; void Fn(int Num, SomeFlags Flags); void foo() { Fn(42, F2 | F3); // No static_cast. } The LLVM_MARK_AS_BITMASK_ENUM macro enables overloads for bitwise operators on SomeFlags. Critically, these operators return the enum type, not its underlying type, so you don't need any static_casts. An advantage of this solution over the previously-proposed BitMask class [0, 1] is that we don't need any wrapper classes -- we can operate directly on the enum itself. The approach here is somewhat similar to OpenOffice's typed_flags_set [2]. But we skirt the need for a wrapper class (and a good deal of complexity) by judicious use of enable_if. We SFINAE on the presence of a particular enumerator (added by the LLVM_MARK_AS_BITMASK_ENUM macro) instead of using a traits class so that it's impossible to use the enum before the overloads are present. The solution here also seamlessly works across multiple namespaces. [0] http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20150622/283369.html [1] http://lists.llvm.org/pipermail/llvm-commits/attachments/20150623/073434b6/attachment.obj [2] https://cgit.freedesktop.org/libreoffice/core/tree/include/o3tl/typed_flags_set.hxx Reviewers: chandlerc, rsmith Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D22279 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275292 91177308-0d34-0410-b5e6-96231b3b80d8 Justin Lebar 4 years ago
3 changed file(s) with 287 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/ADT/BitmaskEnum.h ----------------------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_ADT_BITMASKENUM_H
10 #define LLVM_ADT_BITMASKENUM_H
11
12 #include
13 #include
14 #include
15
16 #include "llvm/Support/MathExtras.h"
17
18 /// LLVM_MARK_AS_BITMASK_ENUM lets you opt in an individual enum type so you can
19 /// perform bitwise operations on it without putting static_cast everywhere.
20 ///
21 /// \code
22 /// enum MyEnum {
23 /// E1 = 1, E2 = 2, E3 = 4, E4 = 8,
24 /// LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ E4)
25 /// };
26 ///
27 /// void Foo() {
28 /// MyEnum A = (E1 | E2) & E3 ^ ~E4; // Look, ma: No static_cast!
29 /// }
30 /// \endcode
31 ///
32 /// Normally when you do a bitwise operation on an enum value, you get back an
33 /// instance of the underlying type (e.g. int). But using this macro, bitwise
34 /// ops on your enum will return you back instances of the enum. This is
35 /// particularly useful for enums which represent a combination of flags.
36 ///
37 /// The parameter to LLVM_MARK_AS_BITMASK_ENUM should be the largest individual
38 /// value in your enum.
39 ///
40 /// All of the enum's values must be non-negative.
41 #define LLVM_MARK_AS_BITMASK_ENUM(LargestValue) \
42 LLVM_BITMASK_LARGEST_ENUMERATOR = LargestValue
43
44 /// LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() pulls the operator overloads used
45 /// by LLVM_MARK_AS_BITMASK_ENUM into the current namespace.
46 ///
47 /// Suppose you have an enum foo::bar::MyEnum. Before using
48 /// LLVM_MARK_AS_BITMASK_ENUM on MyEnum, you must put
49 /// LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() somewhere inside namespace foo or
50 /// namespace foo::bar. This allows the relevant operator overloads to be found
51 /// by ADL.
52 ///
53 /// You don't need to use this macro in namespace llvm; it's done at the bottom
54 /// of this file.
55 #define LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() \
56 using ::llvm::BitmaskEnumDetail::operator~; \
57 using ::llvm::BitmaskEnumDetail::operator|; \
58 using ::llvm::BitmaskEnumDetail::operator&; \
59 using ::llvm::BitmaskEnumDetail::operator^; \
60 using ::llvm::BitmaskEnumDetail::operator|=; \
61 using ::llvm::BitmaskEnumDetail::operator&=; \
62 using ::llvm::BitmaskEnumDetail::operator^=;
63
64 namespace llvm {
65
66 /// Traits class to determine whether an enum has a
67 /// LLVM_BITMASK_LARGEST_ENUMERATOR enumerator.
68 template
69 struct is_bitmask_enum : std::false_type {};
70
71 template
72 struct is_bitmask_enum<
73 E, typename std::enable_if=
74 0>::type> : std::true_type {};
75 namespace BitmaskEnumDetail {
76
77 /// Get a bitmask with 1s in all places up to the high-order bit of E's largest
78 /// value.
79 template typename std::underlying_type::type Mask() {
80 // On overflow, NextPowerOf2 returns zero with the type uint64_t, so
81 // subtracting 1 gives us the mask with all bits set, like we want.
82 return NextPowerOf2(static_cast::type>(
83 E::LLVM_BITMASK_LARGEST_ENUMERATOR)) -
84 1;
85 }
86
87 /// Check that Val is in range for E, and return Val cast to E's underlying
88 /// type.
89 template typename std::underlying_type::type Underlying(E Val) {
90 auto U = static_cast::type>(Val);
91 assert(U >= 0 && "Negative enum values are not allowed.");
92 assert(U <= Mask() && "Enum value too large (or largest val too small?)");
93 return U;
94 }
95
96 template
97 typename = typename std::enable_if::value>::type>
98 E operator~(E Val) {
99 return static_cast(~Underlying(Val) & Mask());
100 }
101
102 template
103 typename = typename std::enable_if::value>::type>
104 E operator|(E LHS, E RHS) {
105 return static_cast(Underlying(LHS) | Underlying(RHS));
106 }
107
108 template
109 typename = typename std::enable_if::value>::type>
110 E operator&(E LHS, E RHS) {
111 return static_cast(Underlying(LHS) & Underlying(RHS));
112 }
113
114 template
115 typename = typename std::enable_if::value>::type>
116 E operator^(E LHS, E RHS) {
117 return static_cast(Underlying(LHS) ^ Underlying(RHS));
118 }
119
120 // |=, &=, and ^= return a reference to LHS, to match the behavior of the
121 // operators on builtin types.
122
123 template
124 typename = typename std::enable_if::value>::type>
125 E &operator|=(E &LHS, E RHS) {
126 LHS = LHS | RHS;
127 return LHS;
128 }
129
130 template
131 typename = typename std::enable_if::value>::type>
132 E &operator&=(E &LHS, E RHS) {
133 LHS = LHS & RHS;
134 return LHS;
135 }
136
137 template
138 typename = typename std::enable_if::value>::type>
139 E &operator^=(E &LHS, E RHS) {
140 LHS = LHS ^ RHS;
141 return LHS;
142 }
143
144 } // namespace BitmaskEnumDetail
145
146 // Enable bitmask enums in namespace ::llvm and all nested namespaces.
147 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
148
149 } // namespace llvm
150
151 #endif
0 //===- llvm/unittest/ADT/BitmaskEnumTest.cpp - BitmaskEnum unit tests -----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/BitmaskEnum.h"
10 #include "gtest/gtest.h"
11
12 using namespace llvm;
13
14 namespace {
15 enum Flags {
16 F0 = 0,
17 F1 = 1,
18 F2 = 2,
19 F3 = 4,
20 F4 = 8,
21 LLVM_MARK_AS_BITMASK_ENUM(F4)
22 };
23
24 TEST(BitmaskEnumTest, BitwiseOr) {
25 Flags f = F1 | F2;
26 EXPECT_EQ(3, f);
27
28 f = f | F3;
29 EXPECT_EQ(7, f);
30 }
31
32 TEST(BitmaskEnumTest, BitwiseOrEquals) {
33 Flags f = F1;
34 f |= F3;
35 EXPECT_EQ(5, f);
36
37 // |= should return a reference to the LHS.
38 f = F2;
39 (f |= F3) = F1;
40 EXPECT_EQ(F1, f);
41 }
42
43 TEST(BitmaskEnumTest, BitwiseAnd) {
44 Flags f = static_cast(3) & F2;
45 EXPECT_EQ(F2, f);
46
47 f = (f | F3) & (F1 | F2 | F3);
48 EXPECT_EQ(6, f);
49 }
50
51 TEST(BitmaskEnumTest, BitwiseAndEquals) {
52 Flags f = F1 | F2 | F3;
53 f &= F1 | F2;
54 EXPECT_EQ(3, f);
55
56 // &= should return a reference to the LHS.
57 (f &= F1) = F3;
58 EXPECT_EQ(F3, f);
59 }
60
61 TEST(BitmaskEnumTest, BitwiseXor) {
62 Flags f = (F1 | F2) ^ (F2 | F3);
63 EXPECT_EQ(5, f);
64
65 f = f ^ F1;
66 EXPECT_EQ(4, f);
67 }
68
69 TEST(BitmaskEnumTest, BitwiseXorEquals) {
70 Flags f = (F1 | F2);
71 f ^= (F2 | F4);
72 EXPECT_EQ(9, f);
73
74 // ^= should return a reference to the LHS.
75 (f ^= F4) = F3;
76 EXPECT_EQ(F3, f);
77 }
78
79 TEST(BitmaskEnumTest, BitwiseNot) {
80 Flags f = ~F1;
81 EXPECT_EQ(14, f); // Largest value for f is 15.
82 EXPECT_EQ(15, ~F0);
83 }
84
85 enum class FlagsClass {
86 F0 = 0,
87 F1 = 1,
88 F2 = 2,
89 F3 = 4,
90 LLVM_MARK_AS_BITMASK_ENUM(F3)
91 };
92
93 TEST(BitmaskEnumTest, ScopedEnum) {
94 FlagsClass f = (FlagsClass::F1 & ~FlagsClass::F0) | FlagsClass::F2;
95 f |= FlagsClass::F3;
96 EXPECT_EQ(7, static_cast(f));
97 }
98
99 struct Container {
100 enum Flags { F0 = 0, F1 = 1, F2 = 2, F3 = 4, LLVM_MARK_AS_BITMASK_ENUM(F3) };
101
102 static Flags getFlags() {
103 Flags f = F0 | F1;
104 f |= F2;
105 return f;
106 }
107 };
108
109 TEST(BitmaskEnumTest, EnumInStruct) { EXPECT_EQ(3, Container::getFlags()); }
110
111 } // namespace
112
113 namespace foo {
114 namespace bar {
115 namespace {
116 enum FlagsInNamespace {
117 F0 = 0,
118 F1 = 1,
119 F2 = 2,
120 F3 = 4,
121 LLVM_MARK_AS_BITMASK_ENUM(F3)
122 };
123 } // namespace
124 } // namespace foo
125 } // namespace bar
126
127 namespace {
128 TEST(BitmaskEnumTest, EnumInNamespace) {
129 foo::bar::FlagsInNamespace f = ~foo::bar::F0 & (foo::bar::F1 | foo::bar::F2);
130 f |= foo::bar::F3;
131 EXPECT_EQ(7, f);
132 }
133 } // namespace
66 APIntTest.cpp
77 APSIntTest.cpp
88 ArrayRefTest.cpp
9 BitmaskEnumTest.cpp
910 BitVectorTest.cpp
1011 DAGDeltaAlgorithmTest.cpp
1112 DeltaAlgorithmTest.cpp