2014-07-23 07:54:41 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-10-04 21:17:13 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2014-04-28 06:46:40 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-04-26 06:34:04 +04:00
|
|
|
#include "mozilla/TypedEnumBits.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
// A rough feature check for is_literal_type. Not very carefully checked.
|
|
|
|
// Feel free to amend as needed.
|
|
|
|
// We leave ANDROID out because it's using stlport which doesn't have std::is_literal_type.
|
|
|
|
#if __cplusplus >= 201103L && !defined(ANDROID)
|
|
|
|
# if defined(__clang__)
|
|
|
|
/*
|
|
|
|
* Per Clang documentation, "Note that marketing version numbers should not
|
|
|
|
* be used to check for language features, as different vendors use different
|
|
|
|
* numbering schemes. Instead, use the feature checking macros."
|
|
|
|
*/
|
|
|
|
# ifndef __has_extension
|
|
|
|
# define __has_extension __has_feature /* compatibility, for older versions of clang */
|
|
|
|
# endif
|
|
|
|
# if __has_extension(is_literal) && __has_include(<type_traits>)
|
|
|
|
# define MOZ_HAVE_IS_LITERAL
|
|
|
|
# endif
|
2015-01-10 01:33:12 +03:00
|
|
|
# elif defined(__GNUC__) || defined(_MSC_VER)
|
2015-01-08 18:38:48 +03:00
|
|
|
# define MOZ_HAVE_IS_LITERAL
|
2014-04-26 06:34:04 +04:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2014-06-13 23:22:16 +04:00
|
|
|
#if defined(MOZ_HAVE_IS_LITERAL) && defined(MOZ_HAVE_CXX11_CONSTEXPR)
|
2014-04-26 06:34:04 +04:00
|
|
|
#include <type_traits>
|
|
|
|
template<typename T>
|
|
|
|
void
|
|
|
|
RequireLiteralType()
|
|
|
|
{
|
|
|
|
static_assert(std::is_literal_type<T>::value, "Expected a literal type");
|
|
|
|
}
|
|
|
|
#else // not MOZ_HAVE_IS_LITERAL
|
|
|
|
template<typename T>
|
|
|
|
void
|
|
|
|
RequireLiteralType()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void
|
|
|
|
RequireLiteralType(const T&)
|
|
|
|
{
|
|
|
|
RequireLiteralType<T>();
|
|
|
|
}
|
2013-10-04 21:17:13 +04:00
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class AutoEnum {
|
2013-10-04 21:17:13 +04:00
|
|
|
A,
|
2014-04-26 06:34:04 +04:00
|
|
|
B = -3,
|
|
|
|
C
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2013-10-04 21:17:13 +04:00
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class CharEnum : char {
|
2013-10-04 21:17:13 +04:00
|
|
|
A,
|
2014-04-26 06:34:04 +04:00
|
|
|
B = 3,
|
|
|
|
C
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2013-10-04 21:17:13 +04:00
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class AutoEnumBitField {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 0x10,
|
|
|
|
B = 0x20,
|
|
|
|
C
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class CharEnumBitField : char {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 0x10,
|
|
|
|
B,
|
|
|
|
C = 0x40
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
|
2013-10-04 21:17:13 +04:00
|
|
|
struct Nested
|
|
|
|
{
|
2015-01-26 01:22:11 +03:00
|
|
|
enum class AutoEnum {
|
2014-04-26 06:34:04 +04:00
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C = -1
|
2015-01-26 01:22:11 +03:00
|
|
|
};
|
2013-10-04 21:17:13 +04:00
|
|
|
|
2015-01-26 01:22:11 +03:00
|
|
|
enum class CharEnum : char {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 4,
|
|
|
|
B,
|
|
|
|
C = 1
|
2015-01-26 01:22:11 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
|
2015-01-26 01:22:11 +03:00
|
|
|
enum class AutoEnumBitField {
|
2014-04-26 06:34:04 +04:00
|
|
|
A,
|
|
|
|
B = 0x20,
|
|
|
|
C
|
2015-01-26 01:22:11 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
|
2015-01-26 01:22:11 +03:00
|
|
|
enum class CharEnumBitField : char {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 1,
|
|
|
|
B = 1,
|
|
|
|
C = 1
|
2015-01-26 01:22:11 +03:00
|
|
|
};
|
2013-10-04 21:17:13 +04:00
|
|
|
};
|
|
|
|
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(AutoEnumBitField)
|
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(CharEnumBitField)
|
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Nested::AutoEnumBitField)
|
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Nested::CharEnumBitField)
|
|
|
|
|
|
|
|
#define MAKE_STANDARD_BITFIELD_FOR_TYPE(IntType) \
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class BitFieldFor_##IntType : IntType { \
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 1, \
|
|
|
|
B = 2, \
|
|
|
|
C = 4, \
|
2015-01-26 01:22:07 +03:00
|
|
|
}; \
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(BitFieldFor_##IntType)
|
|
|
|
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(int8_t)
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(uint8_t)
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(int16_t)
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(uint16_t)
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(int32_t)
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(uint32_t)
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(int64_t)
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(uint64_t)
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(char)
|
|
|
|
typedef signed char signed_char;
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(signed_char)
|
|
|
|
typedef unsigned char unsigned_char;
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(unsigned_char)
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(short)
|
|
|
|
typedef unsigned short unsigned_short;
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(unsigned_short)
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(int)
|
|
|
|
typedef unsigned int unsigned_int;
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(unsigned_int)
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(long)
|
|
|
|
typedef unsigned long unsigned_long;
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(unsigned_long)
|
|
|
|
typedef long long long_long;
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(long_long)
|
|
|
|
typedef unsigned long long unsigned_long_long;
|
|
|
|
MAKE_STANDARD_BITFIELD_FOR_TYPE(unsigned_long_long)
|
|
|
|
|
|
|
|
#undef MAKE_STANDARD_BITFIELD_FOR_TYPE
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void
|
|
|
|
TestNonConvertibilityForOneType()
|
|
|
|
{
|
|
|
|
using mozilla::IsConvertible;
|
2014-04-28 06:46:40 +04:00
|
|
|
|
2014-04-26 06:34:04 +04:00
|
|
|
static_assert(!IsConvertible<T, bool>::value, "should not be convertible");
|
|
|
|
static_assert(!IsConvertible<T, int>::value, "should not be convertible");
|
|
|
|
static_assert(!IsConvertible<T, uint64_t>::value, "should not be convertible");
|
|
|
|
|
|
|
|
static_assert(!IsConvertible<bool, T>::value, "should not be convertible");
|
|
|
|
static_assert(!IsConvertible<int, T>::value, "should not be convertible");
|
|
|
|
static_assert(!IsConvertible<uint64_t, T>::value, "should not be convertible");
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename TypedEnum>
|
|
|
|
void
|
|
|
|
TestTypedEnumBasics()
|
|
|
|
{
|
|
|
|
const TypedEnum a = TypedEnum::A;
|
|
|
|
int unused = int(a);
|
|
|
|
(void) unused;
|
|
|
|
RequireLiteralType(TypedEnum::A);
|
|
|
|
RequireLiteralType(a);
|
|
|
|
TestNonConvertibilityForOneType<TypedEnum>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Op wraps a bitwise binary operator, passed as a char template parameter,
|
2014-07-23 07:54:41 +04:00
|
|
|
// and applies it to its arguments (aT1, aT2). For example,
|
2014-04-26 06:34:04 +04:00
|
|
|
//
|
2014-07-23 07:54:41 +04:00
|
|
|
// Op<'|'>(aT1, aT2)
|
2014-04-26 06:34:04 +04:00
|
|
|
//
|
|
|
|
// is the same as
|
|
|
|
//
|
2014-07-23 07:54:41 +04:00
|
|
|
// aT1 | aT2.
|
2014-04-26 06:34:04 +04:00
|
|
|
//
|
|
|
|
template<char o, typename T1, typename T2>
|
2014-07-23 07:54:41 +04:00
|
|
|
auto Op(const T1& aT1, const T2& aT2)
|
|
|
|
-> decltype(aT1 | aT2) // See the static_assert's below --- the return type
|
|
|
|
// depends solely on the operands type, not on the
|
|
|
|
// choice of operation.
|
2014-04-26 06:34:04 +04:00
|
|
|
{
|
|
|
|
using mozilla::IsSame;
|
2014-07-23 07:54:41 +04:00
|
|
|
static_assert(IsSame<decltype(aT1 | aT2), decltype(aT1 & aT2)>::value,
|
2014-04-26 06:34:04 +04:00
|
|
|
"binary ops should have the same result type");
|
2014-07-23 07:54:41 +04:00
|
|
|
static_assert(IsSame<decltype(aT1 | aT2), decltype(aT1 ^ aT2)>::value,
|
2014-04-26 06:34:04 +04:00
|
|
|
"binary ops should have the same result type");
|
|
|
|
|
|
|
|
static_assert(o == '|' ||
|
|
|
|
o == '&' ||
|
|
|
|
o == '^', "unexpected operator character");
|
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
return o == '|' ? aT1 | aT2
|
|
|
|
: o == '&' ? aT1 & aT2
|
|
|
|
: aT1 ^ aT2;
|
2014-04-26 06:34:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpAssign wraps a bitwise binary operator, passed as a char template
|
|
|
|
// parameter, and applies the corresponding compound-assignment operator to its
|
2014-07-23 07:54:41 +04:00
|
|
|
// arguments (aT1, aT2). For example,
|
2014-04-26 06:34:04 +04:00
|
|
|
//
|
2014-07-23 07:54:41 +04:00
|
|
|
// OpAssign<'|'>(aT1, aT2)
|
2014-04-26 06:34:04 +04:00
|
|
|
//
|
|
|
|
// is the same as
|
|
|
|
//
|
2014-07-23 07:54:41 +04:00
|
|
|
// aT1 |= aT2.
|
2014-04-26 06:34:04 +04:00
|
|
|
//
|
|
|
|
template<char o, typename T1, typename T2>
|
2014-07-23 07:54:41 +04:00
|
|
|
T1& OpAssign(T1& aT1, const T2& aT2)
|
2014-04-26 06:34:04 +04:00
|
|
|
{
|
|
|
|
static_assert(o == '|' ||
|
|
|
|
o == '&' ||
|
|
|
|
o == '^', "unexpected operator character");
|
|
|
|
|
|
|
|
switch (o) {
|
2014-07-23 07:54:41 +04:00
|
|
|
case '|': return aT1 |= aT2;
|
|
|
|
case '&': return aT1 &= aT2;
|
|
|
|
case '^': return aT1 ^= aT2;
|
2014-04-26 06:34:04 +04:00
|
|
|
default: MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests a single binary bitwise operator, using a single set of three operands.
|
|
|
|
// The operations tested are:
|
|
|
|
//
|
2014-07-23 07:54:41 +04:00
|
|
|
// result = aT1 Op aT2;
|
|
|
|
// result Op= aT3;
|
2014-04-26 06:34:04 +04:00
|
|
|
//
|
2014-07-23 07:54:41 +04:00
|
|
|
// Where Op is the operator specified by the char template parameter 'o' and
|
|
|
|
// can be any of '|', '&', '^'.
|
|
|
|
//
|
|
|
|
// Note that the operands aT1, aT2, aT3 are intentionally passed with free
|
|
|
|
// types (separate template parameters for each) because their type may
|
|
|
|
// actually be different from TypedEnum:
|
2014-04-26 06:34:04 +04:00
|
|
|
//
|
|
|
|
// 1) Their type could be CastableTypedEnumResult<TypedEnum> if they are
|
|
|
|
// the result of a bitwise operation themselves;
|
|
|
|
// 2) In the non-c++11 legacy path, the type of enum values is also
|
|
|
|
// different from TypedEnum.
|
|
|
|
//
|
|
|
|
template<typename TypedEnum, char o, typename T1, typename T2, typename T3>
|
2014-07-23 07:54:41 +04:00
|
|
|
void TestBinOp(const T1& aT1, const T2& aT2, const T3& aT3)
|
2014-04-26 06:34:04 +04:00
|
|
|
{
|
|
|
|
typedef typename mozilla::detail::UnsignedIntegerTypeForEnum<TypedEnum>::Type
|
|
|
|
UnsignedIntegerType;
|
|
|
|
|
|
|
|
// Part 1:
|
|
|
|
// Test the bitwise binary operator i.e.
|
2014-07-23 07:54:41 +04:00
|
|
|
// result = aT1 Op aT2;
|
|
|
|
auto result = Op<o>(aT1, aT2);
|
2014-04-26 06:34:04 +04:00
|
|
|
|
|
|
|
typedef decltype(result) ResultType;
|
|
|
|
|
|
|
|
RequireLiteralType<ResultType>();
|
|
|
|
TestNonConvertibilityForOneType<ResultType>();
|
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
UnsignedIntegerType unsignedIntegerResult =
|
|
|
|
Op<o>(UnsignedIntegerType(aT1), UnsignedIntegerType(aT2));
|
2014-04-26 06:34:04 +04:00
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(unsignedIntegerResult == UnsignedIntegerType(result));
|
2014-04-28 20:27:13 +04:00
|
|
|
MOZ_RELEASE_ASSERT(TypedEnum(unsignedIntegerResult) == TypedEnum(result));
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_RELEASE_ASSERT((!unsignedIntegerResult) == (!result));
|
|
|
|
MOZ_RELEASE_ASSERT((!!unsignedIntegerResult) == (!!result));
|
|
|
|
MOZ_RELEASE_ASSERT(bool(unsignedIntegerResult) == bool(result));
|
|
|
|
|
|
|
|
// Part 2:
|
|
|
|
// Test the compound-assignment operator, i.e.
|
2014-07-23 07:54:41 +04:00
|
|
|
// result Op= aT3;
|
2014-04-26 06:34:04 +04:00
|
|
|
TypedEnum newResult = result;
|
2014-07-23 07:54:41 +04:00
|
|
|
OpAssign<o>(newResult, aT3);
|
2014-04-26 06:34:04 +04:00
|
|
|
UnsignedIntegerType unsignedIntegerNewResult = unsignedIntegerResult;
|
2014-07-23 07:54:41 +04:00
|
|
|
OpAssign<o>(unsignedIntegerNewResult, UnsignedIntegerType(aT3));
|
2014-04-28 20:27:13 +04:00
|
|
|
MOZ_RELEASE_ASSERT(TypedEnum(unsignedIntegerNewResult) == newResult);
|
2014-04-26 06:34:04 +04:00
|
|
|
|
|
|
|
// Part 3:
|
|
|
|
// Test additional boolean operators that we unfortunately had to add to
|
|
|
|
// CastableTypedEnumResult at some point to please some compiler,
|
|
|
|
// even though bool convertibility should have been enough.
|
|
|
|
MOZ_RELEASE_ASSERT(result == TypedEnum(result));
|
|
|
|
MOZ_RELEASE_ASSERT(!(result != TypedEnum(result)));
|
|
|
|
MOZ_RELEASE_ASSERT((result && true) == bool(result));
|
|
|
|
MOZ_RELEASE_ASSERT((result && false) == false);
|
|
|
|
MOZ_RELEASE_ASSERT((true && result) == bool(result));
|
|
|
|
MOZ_RELEASE_ASSERT((false && result && false) == false);
|
|
|
|
MOZ_RELEASE_ASSERT((result || false) == bool(result));
|
|
|
|
MOZ_RELEASE_ASSERT((result || true) == true);
|
|
|
|
MOZ_RELEASE_ASSERT((false || result) == bool(result));
|
|
|
|
MOZ_RELEASE_ASSERT((true || result) == true);
|
2016-11-07 10:00:16 +03:00
|
|
|
|
|
|
|
// Part 4:
|
|
|
|
// Test short-circuit evaluation.
|
|
|
|
auto Explode = [] {
|
|
|
|
// This function should never be called. Return an arbitrary value.
|
|
|
|
MOZ_RELEASE_ASSERT(false);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if (result) {
|
|
|
|
MOZ_RELEASE_ASSERT(result || Explode());
|
|
|
|
MOZ_RELEASE_ASSERT(!(!result && Explode()));
|
|
|
|
} else {
|
|
|
|
MOZ_RELEASE_ASSERT(!(result && Explode()));
|
|
|
|
MOZ_RELEASE_ASSERT(!result || Explode());
|
|
|
|
}
|
2014-04-26 06:34:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Similar to TestBinOp but testing the unary ~ operator.
|
|
|
|
template<typename TypedEnum, typename T>
|
2014-07-23 07:54:41 +04:00
|
|
|
void TestTilde(const T& aT)
|
2014-04-26 06:34:04 +04:00
|
|
|
{
|
|
|
|
typedef typename mozilla::detail::UnsignedIntegerTypeForEnum<TypedEnum>::Type
|
|
|
|
UnsignedIntegerType;
|
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
auto result = ~aT;
|
2014-04-26 06:34:04 +04:00
|
|
|
|
|
|
|
typedef decltype(result) ResultType;
|
|
|
|
|
|
|
|
RequireLiteralType<ResultType>();
|
|
|
|
TestNonConvertibilityForOneType<ResultType>();
|
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
UnsignedIntegerType unsignedIntegerResult = ~(UnsignedIntegerType(aT));
|
2014-04-26 06:34:04 +04:00
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(unsignedIntegerResult == UnsignedIntegerType(result));
|
2014-04-28 20:27:13 +04:00
|
|
|
MOZ_RELEASE_ASSERT(TypedEnum(unsignedIntegerResult) == TypedEnum(result));
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_RELEASE_ASSERT((!unsignedIntegerResult) == (!result));
|
|
|
|
MOZ_RELEASE_ASSERT((!!unsignedIntegerResult) == (!!result));
|
|
|
|
MOZ_RELEASE_ASSERT(bool(unsignedIntegerResult) == bool(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper dispatching a given triple of operands to all operator-specific
|
|
|
|
// testing functions.
|
|
|
|
template<typename TypedEnum, typename T1, typename T2, typename T3>
|
2014-07-23 07:54:41 +04:00
|
|
|
void TestAllOpsForGivenOperands(const T1& aT1, const T2& aT2, const T3& aT3)
|
2014-04-26 06:34:04 +04:00
|
|
|
{
|
2014-07-23 07:54:41 +04:00
|
|
|
TestBinOp<TypedEnum, '|'>(aT1, aT2, aT3);
|
|
|
|
TestBinOp<TypedEnum, '&'>(aT1, aT2, aT3);
|
|
|
|
TestBinOp<TypedEnum, '^'>(aT1, aT2, aT3);
|
|
|
|
TestTilde<TypedEnum>(aT1);
|
2014-04-26 06:34:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper building various triples of operands using a given operator,
|
|
|
|
// and testing all operators with them.
|
|
|
|
template<typename TypedEnum, char o>
|
|
|
|
void TestAllOpsForOperandsBuiltUsingGivenOp()
|
|
|
|
{
|
|
|
|
// The type of enum values like TypedEnum::A may be different from
|
|
|
|
// TypedEnum. That is the case in the legacy non-C++11 path. We want to
|
|
|
|
// ensure good test coverage even when these two types are distinct.
|
|
|
|
// To that effect, we have both 'auto' typed variables, preserving the
|
|
|
|
// original type of enum values, and 'plain' typed variables, that
|
|
|
|
// are plain TypedEnum's.
|
|
|
|
|
|
|
|
const TypedEnum a_plain = TypedEnum::A;
|
|
|
|
const TypedEnum b_plain = TypedEnum::B;
|
|
|
|
const TypedEnum c_plain = TypedEnum::C;
|
|
|
|
|
|
|
|
auto a_auto = TypedEnum::A;
|
|
|
|
auto b_auto = TypedEnum::B;
|
|
|
|
auto c_auto = TypedEnum::C;
|
|
|
|
|
|
|
|
auto ab_plain = Op<o>(a_plain, b_plain);
|
|
|
|
auto bc_plain = Op<o>(b_plain, c_plain);
|
|
|
|
auto ab_auto = Op<o>(a_auto, b_auto);
|
|
|
|
auto bc_auto = Op<o>(b_auto, c_auto);
|
|
|
|
|
|
|
|
// On each row below, we pass a triple of operands. Keep in mind that this
|
2014-07-23 07:54:41 +04:00
|
|
|
// is going to be received as (aT1, aT2, aT3) and the actual tests performed
|
2014-04-26 06:34:04 +04:00
|
|
|
// will be of the form
|
|
|
|
//
|
2014-07-23 07:54:41 +04:00
|
|
|
// result = aT1 Op aT2;
|
|
|
|
// result Op= aT3;
|
2014-04-26 06:34:04 +04:00
|
|
|
//
|
2014-07-23 07:54:41 +04:00
|
|
|
// For this reason, we carefully ensure that the values of (aT1, aT2)
|
2014-04-26 06:34:04 +04:00
|
|
|
// systematically cover all types of such pairs; to limit complexity,
|
2014-07-23 07:54:41 +04:00
|
|
|
// we are not so careful with aT3, and we just try to pass aT3's
|
2014-04-26 06:34:04 +04:00
|
|
|
// that may lead to nontrivial bitwise operations.
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(a_plain, b_plain, c_plain);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(a_plain, bc_plain, b_auto);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(ab_plain, c_plain, a_plain);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(ab_plain, bc_plain, a_auto);
|
|
|
|
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(a_plain, b_auto, c_plain);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(a_plain, bc_auto, b_auto);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(ab_plain, c_auto, a_plain);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(ab_plain, bc_auto, a_auto);
|
|
|
|
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(a_auto, b_plain, c_plain);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(a_auto, bc_plain, b_auto);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(ab_auto, c_plain, a_plain);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(ab_auto, bc_plain, a_auto);
|
|
|
|
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(a_auto, b_auto, c_plain);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(a_auto, bc_auto, b_auto);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(ab_auto, c_auto, a_plain);
|
|
|
|
TestAllOpsForGivenOperands<TypedEnum>(ab_auto, bc_auto, a_auto);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests all bitwise operations on a given TypedEnum bitfield.
|
|
|
|
template<typename TypedEnum>
|
|
|
|
void
|
|
|
|
TestTypedEnumBitField()
|
|
|
|
{
|
|
|
|
TestTypedEnumBasics<TypedEnum>();
|
|
|
|
|
|
|
|
TestAllOpsForOperandsBuiltUsingGivenOp<TypedEnum, '|'>();
|
|
|
|
TestAllOpsForOperandsBuiltUsingGivenOp<TypedEnum, '&'>();
|
|
|
|
TestAllOpsForOperandsBuiltUsingGivenOp<TypedEnum, '^'>();
|
|
|
|
}
|
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
// Checks that enum bitwise expressions have the same non-convertibility
|
|
|
|
// properties as c++11 enum classes do, i.e. not implicitly convertible to
|
|
|
|
// anything (though *explicitly* convertible).
|
2014-04-26 06:34:04 +04:00
|
|
|
void TestNoConversionsBetweenUnrelatedTypes()
|
|
|
|
{
|
|
|
|
using mozilla::IsConvertible;
|
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
// Two typed enum classes having the same underlying integer type, to ensure
|
|
|
|
// that we would catch bugs accidentally allowing conversions in that case.
|
2014-04-26 06:34:04 +04:00
|
|
|
typedef CharEnumBitField T1;
|
|
|
|
typedef Nested::CharEnumBitField T2;
|
|
|
|
|
|
|
|
static_assert(!IsConvertible<T1, T2>::value,
|
|
|
|
"should not be convertible");
|
|
|
|
static_assert(!IsConvertible<T1, decltype(T2::A)>::value,
|
|
|
|
"should not be convertible");
|
|
|
|
static_assert(!IsConvertible<T1, decltype(T2::A | T2::B)>::value,
|
|
|
|
"should not be convertible");
|
|
|
|
|
|
|
|
static_assert(!IsConvertible<decltype(T1::A), T2>::value,
|
|
|
|
"should not be convertible");
|
|
|
|
static_assert(!IsConvertible<decltype(T1::A), decltype(T2::A)>::value,
|
|
|
|
"should not be convertible");
|
|
|
|
static_assert(!IsConvertible<decltype(T1::A), decltype(T2::A | T2::B)>::value,
|
|
|
|
"should not be convertible");
|
|
|
|
|
|
|
|
static_assert(!IsConvertible<decltype(T1::A | T1::B), T2>::value,
|
|
|
|
"should not be convertible");
|
|
|
|
static_assert(!IsConvertible<decltype(T1::A | T1::B), decltype(T2::A)>::value,
|
|
|
|
"should not be convertible");
|
|
|
|
static_assert(!IsConvertible<decltype(T1::A | T1::B), decltype(T2::A | T2::B)>::value,
|
|
|
|
"should not be convertible");
|
|
|
|
}
|
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class Int8EnumWithHighBits : int8_t {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 0x20,
|
|
|
|
B = 0x40
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Int8EnumWithHighBits)
|
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class Uint8EnumWithHighBits : uint8_t {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 0x40,
|
|
|
|
B = 0x80
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Uint8EnumWithHighBits)
|
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class Int16EnumWithHighBits : int16_t {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 0x2000,
|
|
|
|
B = 0x4000
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Int16EnumWithHighBits)
|
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class Uint16EnumWithHighBits : uint16_t {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 0x4000,
|
|
|
|
B = 0x8000
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Uint16EnumWithHighBits)
|
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class Int32EnumWithHighBits : int32_t {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 0x20000000,
|
|
|
|
B = 0x40000000
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Int32EnumWithHighBits)
|
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class Uint32EnumWithHighBits : uint32_t {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 0x40000000u,
|
|
|
|
B = 0x80000000u
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Uint32EnumWithHighBits)
|
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class Int64EnumWithHighBits : int64_t {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 0x2000000000000000ll,
|
|
|
|
B = 0x4000000000000000ll
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Int64EnumWithHighBits)
|
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class Uint64EnumWithHighBits : uint64_t {
|
2014-04-26 06:34:04 +04:00
|
|
|
A = 0x4000000000000000ull,
|
|
|
|
B = 0x8000000000000000ull
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-04-26 06:34:04 +04:00
|
|
|
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Uint64EnumWithHighBits)
|
|
|
|
|
|
|
|
// Checks that we don't accidentally truncate high bits by coercing to the wrong
|
|
|
|
// integer type internally when implementing bitwise ops.
|
|
|
|
template<typename EnumType, typename IntType>
|
|
|
|
void TestIsNotTruncated()
|
|
|
|
{
|
|
|
|
EnumType a = EnumType::A;
|
|
|
|
EnumType b = EnumType::B;
|
|
|
|
MOZ_RELEASE_ASSERT(IntType(a));
|
|
|
|
MOZ_RELEASE_ASSERT(IntType(b));
|
|
|
|
MOZ_RELEASE_ASSERT(a | EnumType::B);
|
|
|
|
MOZ_RELEASE_ASSERT(a | b);
|
|
|
|
MOZ_RELEASE_ASSERT(EnumType::A | EnumType::B);
|
|
|
|
EnumType c = EnumType::A | EnumType::B;
|
|
|
|
MOZ_RELEASE_ASSERT(IntType(c));
|
|
|
|
MOZ_RELEASE_ASSERT(c & c);
|
|
|
|
MOZ_RELEASE_ASSERT(c | c);
|
|
|
|
MOZ_RELEASE_ASSERT(c == (EnumType::A | EnumType::B));
|
|
|
|
MOZ_RELEASE_ASSERT(a != (EnumType::A | EnumType::B));
|
|
|
|
MOZ_RELEASE_ASSERT(b != (EnumType::A | EnumType::B));
|
|
|
|
MOZ_RELEASE_ASSERT(c & EnumType::A);
|
|
|
|
MOZ_RELEASE_ASSERT(c & EnumType::B);
|
|
|
|
EnumType d = EnumType::A;
|
|
|
|
d |= EnumType::B;
|
|
|
|
MOZ_RELEASE_ASSERT(d == c);
|
|
|
|
}
|
2013-10-04 21:17:13 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
2014-04-26 06:34:04 +04:00
|
|
|
TestTypedEnumBasics<AutoEnum>();
|
|
|
|
TestTypedEnumBasics<CharEnum>();
|
|
|
|
TestTypedEnumBasics<Nested::AutoEnum>();
|
|
|
|
TestTypedEnumBasics<Nested::CharEnum>();
|
|
|
|
|
|
|
|
TestTypedEnumBitField<AutoEnumBitField>();
|
|
|
|
TestTypedEnumBitField<CharEnumBitField>();
|
|
|
|
TestTypedEnumBitField<Nested::AutoEnumBitField>();
|
|
|
|
TestTypedEnumBitField<Nested::CharEnumBitField>();
|
|
|
|
|
|
|
|
TestTypedEnumBitField<BitFieldFor_uint8_t>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_int8_t>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_uint16_t>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_int16_t>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_uint32_t>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_int32_t>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_uint64_t>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_int64_t>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_char>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_signed_char>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_unsigned_char>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_short>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_unsigned_short>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_int>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_unsigned_int>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_long>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_unsigned_long>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_long_long>();
|
|
|
|
TestTypedEnumBitField<BitFieldFor_unsigned_long_long>();
|
|
|
|
|
|
|
|
TestNoConversionsBetweenUnrelatedTypes();
|
|
|
|
|
|
|
|
TestIsNotTruncated<Int8EnumWithHighBits, int8_t>();
|
|
|
|
TestIsNotTruncated<Int16EnumWithHighBits, int16_t>();
|
|
|
|
TestIsNotTruncated<Int32EnumWithHighBits, int32_t>();
|
|
|
|
TestIsNotTruncated<Int64EnumWithHighBits, int64_t>();
|
|
|
|
TestIsNotTruncated<Uint8EnumWithHighBits, uint8_t>();
|
|
|
|
TestIsNotTruncated<Uint16EnumWithHighBits, uint16_t>();
|
|
|
|
TestIsNotTruncated<Uint32EnumWithHighBits, uint32_t>();
|
|
|
|
TestIsNotTruncated<Uint64EnumWithHighBits, uint64_t>();
|
|
|
|
|
|
|
|
return 0;
|
2013-10-04 21:17:13 +04:00
|
|
|
}
|