Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 9 | // This file defines the AlignedCharArrayUnion class. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_SUPPORT_ALIGNOF_H |
| 14 | #define LLVM_SUPPORT_ALIGNOF_H |
| 15 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 16 | #include <type_traits> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 17 | |
| 18 | namespace llvm { |
| 19 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 20 | /// A suitably aligned and sized character array member which can hold elements |
| 21 | /// of any type. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 22 | /// |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 23 | /// This template is equivalent to std::aligned_union_t<1, ...>, but we cannot |
| 24 | /// use it due to a bug in the MSVC x86 compiler: |
| 25 | /// https://github.com/microsoft/STL/issues/1533 |
| 26 | /// Using `alignas` here works around the bug. |
| 27 | template <typename T, typename... Ts> struct AlignedCharArrayUnion { |
| 28 | using AlignedUnion = std::aligned_union_t<1, T, Ts...>; |
| 29 | alignas(alignof(AlignedUnion)) char buffer[sizeof(AlignedUnion)]; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 30 | }; |
| 31 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 32 | } // end namespace llvm |
| 33 | |
| 34 | #endif // LLVM_SUPPORT_ALIGNOF_H |