Skip to content

Commit

Permalink
Add BitWidth trait to BitmaskEnum, and use for clang DependenceFlags.…
Browse files Browse the repository at this point in the history
… NFC

Reviewers: hokein

Subscribers: dexonsmith, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D76942
  • Loading branch information
sam-mccall committed Mar 27, 2020
1 parent 0f56bbc commit 6b3bede
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
5 changes: 1 addition & 4 deletions clang/include/clang/AST/DependenceFlags.h
Expand Up @@ -36,7 +36,6 @@ struct ExprDependenceScope {
};
};
using ExprDependence = ExprDependenceScope::ExprDependence;
static constexpr unsigned ExprDependenceBits = 5;

struct TypeDependenceScope {
enum TypeDependence : uint8_t {
Expand All @@ -62,7 +61,6 @@ struct TypeDependenceScope {
};
};
using TypeDependence = TypeDependenceScope::TypeDependence;
static constexpr unsigned TypeDependenceBits = 4;

#define LLVM_COMMON_DEPENDENCE(NAME) \
struct NAME##Scope { \
Expand All @@ -78,8 +76,7 @@ static constexpr unsigned TypeDependenceBits = 4;
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/Dependent) \
}; \
}; \
using NAME = NAME##Scope::NAME; \
static constexpr unsigned NAME##Bits = 3;
using NAME = NAME##Scope::NAME;

LLVM_COMMON_DEPENDENCE(NestedNameSpecifierDependence)
LLVM_COMMON_DEPENDENCE(TemplateNameDependence)
Expand Down
5 changes: 3 additions & 2 deletions clang/include/clang/AST/Stmt.h
Expand Up @@ -21,6 +21,7 @@
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator.h"
Expand Down Expand Up @@ -310,9 +311,9 @@ class alignas(void *) Stmt {

unsigned ValueKind : 2;
unsigned ObjectKind : 3;
unsigned /*ExprDependence*/ Dependent : ExprDependenceBits;
unsigned /*ExprDependence*/ Dependent : llvm::BitWidth<ExprDependence>;
};
enum { NumExprBits = NumStmtBits + 5 + ExprDependenceBits };
enum { NumExprBits = NumStmtBits + 5 + llvm::BitWidth<ExprDependence> };

class ConstantExprBitfields {
friend class ASTStmtReader;
Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/AST/Type.h
Expand Up @@ -1467,7 +1467,7 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
unsigned TC : 8;

/// Store information on the type dependency.
/*TypeDependence*/ unsigned Dependence : TypeDependenceBits;
unsigned Dependence : llvm::BitWidth<TypeDependence>;

/// True if the cache (i.e. the bitfields here starting with
/// 'Cache') is valid.
Expand Down Expand Up @@ -1496,7 +1496,7 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
return CachedLocalOrUnnamed;
}
};
enum { NumTypeBits = 8 + TypeDependenceBits + 6 };
enum { NumTypeBits = 8 + llvm::BitWidth<TypeDependence> + 6 };

protected:
// These classes allow subclasses to somewhat cleanly pack bitfields
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Serialization/ASTReaderStmt.cpp
Expand Up @@ -50,6 +50,7 @@
#include "clang/Lex/Token.h"
#include "clang/Serialization/ASTBitCodes.h"
#include "clang/Serialization/ASTRecordReader.h"
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -107,7 +108,7 @@ namespace clang {
/// The number of record fields required for the Expr class
/// itself.
static const unsigned NumExprFields =
NumStmtFields + ExprDependenceBits + 3;
NumStmtFields + llvm::BitWidth<ExprDependence> + 3;

/// Read and initialize a ExplicitTemplateArgumentList structure.
void ReadTemplateKWAndArgsInfo(ASTTemplateKWAndArgsInfo &Args,
Expand Down
8 changes: 8 additions & 0 deletions llvm/include/llvm/ADT/BitmaskEnum.h
Expand Up @@ -94,6 +94,10 @@ template <typename E> std::underlying_type_t<E> Underlying(E Val) {
return U;
}

constexpr unsigned bitWidth(uint64_t Value) {
return Value ? 1 + bitWidth(Value >> 1) : 0;
}

template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
E operator~(E Val) {
return static_cast<E>(~Underlying(Val) & Mask<E>());
Expand Down Expand Up @@ -139,6 +143,10 @@ E &operator^=(E &LHS, E RHS) {

// Enable bitmask enums in namespace ::llvm and all nested namespaces.
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
constexpr unsigned BitWidth = BitmaskEnumDetail::bitWidth(uint64_t{
static_cast<std::underlying_type_t<E>>(
E::LLVM_BITMASK_LARGEST_ENUMERATOR)});

} // namespace llvm

Expand Down

0 comments on commit 6b3bede

Please sign in to comment.