LLVM  6.0.0svn
Classes | Namespaces | Macros | Functions
BitmaskEnum.h File Reference
#include <cassert>
#include <type_traits>
#include <utility>
#include "llvm/Support/MathExtras.h"
Include dependency graph for BitmaskEnum.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  llvm::is_bitmask_enum< E, Enable >
 Traits class to determine whether an enum has a LLVM_BITMASK_LARGEST_ENUMERATOR enumerator. More...
 
struct  llvm::is_bitmask_enum< E, Enable >::type
 

Namespaces

 llvm
 Compute iterated dominance frontiers using a linear time algorithm.
 
 llvm::BitmaskEnumDetail
 

Macros

#define LLVM_MARK_AS_BITMASK_ENUM(LargestValue)   LLVM_BITMASK_LARGEST_ENUMERATOR = LargestValue
 LLVM_MARK_AS_BITMASK_ENUM lets you opt in an individual enum type so you can perform bitwise operations on it without putting static_cast everywhere. More...
 
#define LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE()
 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() pulls the operator overloads used by LLVM_MARK_AS_BITMASK_ENUM into the current namespace. More...
 

Functions

template<typename E >
std::underlying_type< E >::type llvm::BitmaskEnumDetail::Mask ()
 Get a bitmask with 1s in all places up to the high-order bit of E's largest value. More...
 
template<typename E >
std::underlying_type< E >::type llvm::BitmaskEnumDetail::Underlying (E Val)
 Check that Val is in range for E, and return Val cast to E's underlying type. More...
 
template<typename E , typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
E llvm::BitmaskEnumDetail::operator~ (E Val)
 
template<typename E , typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
E llvm::BitmaskEnumDetail::operator| (E LHS, E RHS)
 
template<typename E , typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
E llvm::BitmaskEnumDetail::operator & (E LHS, E RHS)
 
template<typename E , typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
E llvm::BitmaskEnumDetail::operator^ (E LHS, E RHS)
 
template<typename E , typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
Ellvm::BitmaskEnumDetail::operator|= (E &LHS, E RHS)
 
template<typename E , typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
Ellvm::BitmaskEnumDetail::operator &= (E &LHS, E RHS)
 
template<typename E , typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
Ellvm::BitmaskEnumDetail::operator^= (E &LHS, E RHS)
 
 llvm::LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE ()
 

Macro Definition Documentation

◆ LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE

#define LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE ( )
Value:
using ::llvm::BitmaskEnumDetail::operator~; \
using ::llvm::BitmaskEnumDetail::operator|; \
using ::llvm::BitmaskEnumDetail::operator&; \
using ::llvm::BitmaskEnumDetail::operator^; \
using ::llvm::BitmaskEnumDetail::operator|=; \
using ::llvm::BitmaskEnumDetail::operator&=; \
/* Force a semicolon at the end of this macro. */ \
using ::llvm::BitmaskEnumDetail::operator^=

LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() pulls the operator overloads used by LLVM_MARK_AS_BITMASK_ENUM into the current namespace.

Suppose you have an enum foo::bar::MyEnum. Before using LLVM_MARK_AS_BITMASK_ENUM on MyEnum, you must put LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() somewhere inside namespace foo or namespace foo::bar. This allows the relevant operator overloads to be found by ADL.

You don't need to use this macro in namespace llvm; it's done at the bottom of this file.

Definition at line 56 of file BitmaskEnum.h.

◆ LLVM_MARK_AS_BITMASK_ENUM

#define LLVM_MARK_AS_BITMASK_ENUM (   LargestValue)    LLVM_BITMASK_LARGEST_ENUMERATOR = LargestValue

LLVM_MARK_AS_BITMASK_ENUM lets you opt in an individual enum type so you can perform bitwise operations on it without putting static_cast everywhere.

enum MyEnum {
E1 = 1, E2 = 2, E3 = 4, E4 = 8,
LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ E4)
};
void Foo() {
MyEnum A = (E1 | E2) & E3 ^ ~E4; // Look, ma: No static_cast!
}

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.

The parameter to LLVM_MARK_AS_BITMASK_ENUM should be the largest individual value in your enum.

All of the enum's values must be non-negative.

Definition at line 42 of file BitmaskEnum.h.