diff options
author | Howard Hinnant <hhinnant@apple.com> | 2013-04-22 19:37:49 +0000 |
---|---|---|
committer | Howard Hinnant <hhinnant@apple.com> | 2013-04-22 19:37:49 +0000 |
commit | 20428e94e0b59b3bbfbafbdde7b461cf19f65dd0 (patch) | |
tree | 5638802d67a10c25ac5e97ade34ed9b70faeba9d | |
parent | 74679a93b2a4abb4f11f625340b819ced7164773 (diff) | |
download | bcm5719-llvm-20428e94e0b59b3bbfbafbdde7b461cf19f65dd0.tar.gz bcm5719-llvm-20428e94e0b59b3bbfbafbdde7b461cf19f65dd0.zip |
Somehow aligned_union got dropped through the cracks. This adds it. Did a drive-by fix of alignment_of while I was in the neighborhood.
llvm-svn: 180036
4 files changed, 107 insertions, 11 deletions
diff --git a/libcxx/include/type_traits b/libcxx/include/type_traits index 7178978f5cc..ab0e2220162 100644 --- a/libcxx/include/type_traits +++ b/libcxx/include/type_traits @@ -129,6 +129,7 @@ namespace std template <class T> struct alignment_of; template <size_t Len, size_t Align = most_stringent_alignment_requirement> struct aligned_storage; + template <size_t Len, class... Types> struct aligned_union; template <class T> struct decay; template <class... T> struct common_type; @@ -828,10 +829,8 @@ template <class _Tp> struct _LIBCPP_TYPE_VIS has_virtual_destructor // alignment_of -template <class _Tp> struct __alignment_of {_Tp __lx;}; - template <class _Tp> struct _LIBCPP_TYPE_VIS alignment_of - : public integral_constant<size_t, __alignof__(__alignment_of<typename remove_all_extents<_Tp>::type>)> {}; + : public integral_constant<size_t, __alignof__(_Tp)> {}; // aligned_storage @@ -960,6 +959,38 @@ _CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x4000); #undef _CREATE_ALIGNED_STORAGE_SPECIALIZATION +#ifndef _LIBCPP_HAS_NO_VARIADICS + +// aligned_union + +template <size_t _I0, size_t ..._In> +struct __static_max; + +template <size_t _I0> +struct __static_max<_I0> +{ + static const size_t value = _I0; +}; + +template <size_t _I0, size_t _I1, size_t ..._In> +struct __static_max<_I0, _I1, _In...> +{ + static const size_t value = _I0 >= _I1 ? __static_max<_I0, _In...>::value : + __static_max<_I1, _In...>::value; +}; + +template <size_t _Len, class _Type0, class ..._Types> +struct aligned_union +{ + static const size_t alignment_value = __static_max<__alignof__(_Type0), + __alignof__(_Types)...>::value; + static const size_t __len = __static_max<_Len, sizeof(_Type0), + sizeof(_Types)...>::value; + typedef typename aligned_storage<__len, alignment_value>::type type; +}; + +#endif // _LIBCPP_HAS_NO_VARIADICS + // __promote template <class _A1, class _A2 = void, class _A3 = void, diff --git a/libcxx/test/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp b/libcxx/test/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp index 845c7620f10..323fd5e4cfb 100644 --- a/libcxx/test/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp +++ b/libcxx/test/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp @@ -92,17 +92,17 @@ int main() } { typedef std::aligned_storage<8>::type T1; - static_assert(std::alignment_of<T1>::value == (sizeof(long) == 4 ? 4 : 8), ""); + static_assert(std::alignment_of<T1>::value == 8, ""); static_assert(sizeof(T1) == 8, ""); } { typedef std::aligned_storage<9>::type T1; - static_assert(std::alignment_of<T1>::value == (sizeof(long) == 4 ? 4 : 8), ""); - static_assert(sizeof(T1) == (sizeof(long) == 4 ? 12 : 16), ""); + static_assert(std::alignment_of<T1>::value == 8, ""); + static_assert(sizeof(T1) == 16, ""); } { typedef std::aligned_storage<15>::type T1; - static_assert(std::alignment_of<T1>::value == (sizeof(long) == 4 ? 4 : 8), ""); + static_assert(std::alignment_of<T1>::value == 8, ""); static_assert(sizeof(T1) == 16, ""); } { @@ -117,7 +117,7 @@ int main() } { typedef std::aligned_storage<10>::type T1; - static_assert(std::alignment_of<T1>::value == (sizeof(long) == 4 ? 4 : 8), ""); - static_assert(sizeof(T1) == (sizeof(long) == 4 ? 12 : 16), ""); + static_assert(std::alignment_of<T1>::value == 8, ""); + static_assert(sizeof(T1) == 16, ""); } } diff --git a/libcxx/test/utilities/meta/meta.trans/meta.trans.other/aligned_union.pass.cpp b/libcxx/test/utilities/meta/meta.trans/meta.trans.other/aligned_union.pass.cpp new file mode 100644 index 00000000000..b07a06479d6 --- /dev/null +++ b/libcxx/test/utilities/meta/meta.trans/meta.trans.other/aligned_union.pass.cpp @@ -0,0 +1,65 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// type_traits + +// aligned_union<size_t Len, class ...Types> + +#include <type_traits> + +int main() +{ +#ifndef _LIBCPP_HAS_NO_VARIADICS + { + typedef std::aligned_union<10, char >::type T1; + static_assert(std::alignment_of<T1>::value == 1, ""); + static_assert(sizeof(T1) == 10, ""); + } + { + typedef std::aligned_union<10, short >::type T1; + static_assert(std::alignment_of<T1>::value == 2, ""); + static_assert(sizeof(T1) == 10, ""); + } + { + typedef std::aligned_union<10, int >::type T1; + static_assert(std::alignment_of<T1>::value == 4, ""); + static_assert(sizeof(T1) == 12, ""); + } + { + typedef std::aligned_union<10, double >::type T1; + static_assert(std::alignment_of<T1>::value == 8, ""); + static_assert(sizeof(T1) == 16, ""); + } + { + typedef std::aligned_union<10, short, char >::type T1; + static_assert(std::alignment_of<T1>::value == 2, ""); + static_assert(sizeof(T1) == 10, ""); + } + { + typedef std::aligned_union<10, char, short >::type T1; + static_assert(std::alignment_of<T1>::value == 2, ""); + static_assert(sizeof(T1) == 10, ""); + } + { + typedef std::aligned_union<2, int, char, short >::type T1; + static_assert(std::alignment_of<T1>::value == 4, ""); + static_assert(sizeof(T1) == 4, ""); + } + { + typedef std::aligned_union<2, char, int, short >::type T1; + static_assert(std::alignment_of<T1>::value == 4, ""); + static_assert(sizeof(T1) == 4, ""); + } + { + typedef std::aligned_union<2, char, short, int >::type T1; + static_assert(std::alignment_of<T1>::value == 4, ""); + static_assert(sizeof(T1) == 4, ""); + } +#endif +} diff --git a/libcxx/test/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp b/libcxx/test/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp index 87c22248813..c896aa20229 100644 --- a/libcxx/test/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp +++ b/libcxx/test/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp @@ -30,13 +30,13 @@ public: int main() { - test_alignment_of<int&, sizeof(long) == 4 ? 4 : 8>(); + test_alignment_of<int&, 4>(); test_alignment_of<Class, 1>(); test_alignment_of<int*, sizeof(long) == 4 ? 4 : 8>(); test_alignment_of<const int*, sizeof(long) == 4 ? 4 : 8>(); test_alignment_of<char[3], 1>(); test_alignment_of<int, 4>(); - test_alignment_of<double, sizeof(long) == 4 ? 4 : 8>(); + test_alignment_of<double, 8>(); test_alignment_of<bool, 1>(); test_alignment_of<unsigned, 4>(); } |