Reapply part of r237975, "Fix Clang -Wmissing-override warning", except for DIContext...
[oota-llvm.git] / include / llvm / Support / AlignOf.h
1 //===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the AlignOf function that computes alignments for
11 // arbitrary types.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_ALIGNOF_H
16 #define LLVM_SUPPORT_ALIGNOF_H
17
18 #include "llvm/Support/Compiler.h"
19 #include <cstddef>
20
21 namespace llvm {
22 template <typename T>
23 struct AlignmentCalcImpl {
24   char x;
25 #if defined(_MSC_VER)
26 // Disables "structure was padded due to __declspec(align())" warnings that are
27 // generated by any class using AlignOf<T> with a manually specified alignment.
28 // Although the warning is disabled in the LLVM project we need this pragma
29 // as AlignOf.h is a published support header that's available for use
30 // out-of-tree, and we would like that to compile cleanly at /W4.
31 #pragma warning(suppress : 4324)
32 #endif
33   T t;
34 private:
35   AlignmentCalcImpl() {} // Never instantiate.
36 };
37
38 /// AlignOf - A templated class that contains an enum value representing
39 ///  the alignment of the template argument.  For example,
40 ///  AlignOf<int>::Alignment represents the alignment of type "int".  The
41 ///  alignment calculated is the minimum alignment, and not necessarily
42 ///  the "desired" alignment returned by GCC's __alignof__ (for example).  Note
43 ///  that because the alignment is an enum value, it can be used as a
44 ///  compile-time constant (e.g., for template instantiation).
45 template <typename T>
46 struct AlignOf {
47   enum { Alignment =
48          static_cast<unsigned int>(sizeof(AlignmentCalcImpl<T>) - sizeof(T)) };
49
50   enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 };
51   enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 };
52   enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 };
53   enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 };
54
55   enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 };
56   enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 };
57   enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 };
58   enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 };
59 };
60
61 /// alignOf - A templated function that returns the minimum alignment of
62 ///  of a type.  This provides no extra functionality beyond the AlignOf
63 ///  class besides some cosmetic cleanliness.  Example usage:
64 ///  alignOf<int>() returns the alignment of an int.
65 template <typename T>
66 inline unsigned alignOf() { return AlignOf<T>::Alignment; }
67
68 /// \struct AlignedCharArray
69 /// \brief Helper for building an aligned character array type.
70 ///
71 /// This template is used to explicitly build up a collection of aligned
72 /// character array types. We have to build these up using a macro and explicit
73 /// specialization to cope with old versions of MSVC and GCC where only an
74 /// integer literal can be used to specify an alignment constraint. Once built
75 /// up here, we can then begin to indirect between these using normal C++
76 /// template parameters.
77
78 // MSVC requires special handling here.
79 #ifndef _MSC_VER
80
81 #if __has_feature(cxx_alignas)
82 template<std::size_t Alignment, std::size_t Size>
83 struct AlignedCharArray {
84   alignas(Alignment) char buffer[Size];
85 };
86
87 #elif defined(__GNUC__) || defined(__IBM_ATTRIBUTES)
88 /// \brief Create a type with an aligned char buffer.
89 template<std::size_t Alignment, std::size_t Size>
90 struct AlignedCharArray;
91
92 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
93   template<std::size_t Size> \
94   struct AlignedCharArray<x, Size> { \
95     __attribute__((aligned(x))) char buffer[Size]; \
96   };
97
98 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1)
99 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2)
100 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4)
101 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8)
102 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
103 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
104 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
105 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
106
107 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
108
109 #else
110 # error No supported align as directive.
111 #endif
112
113 #else // _MSC_VER
114
115 /// \brief Create a type with an aligned char buffer.
116 template<std::size_t Alignment, std::size_t Size>
117 struct AlignedCharArray;
118
119 // We provide special variations of this template for the most common
120 // alignments because __declspec(align(...)) doesn't actually work when it is
121 // a member of a by-value function argument in MSVC, even if the alignment
122 // request is something reasonably like 8-byte or 16-byte. Note that we can't
123 // even include the declspec with the union that forces the alignment because
124 // MSVC warns on the existence of the declspec despite the union member forcing
125 // proper alignment.
126
127 template<std::size_t Size>
128 struct AlignedCharArray<1, Size> {
129   union {
130     char aligned;
131     char buffer[Size];
132   };
133 };
134
135 template<std::size_t Size>
136 struct AlignedCharArray<2, Size> {
137   union {
138     short aligned;
139     char buffer[Size];
140   };
141 };
142
143 template<std::size_t Size>
144 struct AlignedCharArray<4, Size> {
145   union {
146     int aligned;
147     char buffer[Size];
148   };
149 };
150
151 template<std::size_t Size>
152 struct AlignedCharArray<8, Size> {
153   union {
154     double aligned;
155     char buffer[Size];
156   };
157 };
158
159
160 // The rest of these are provided with a __declspec(align(...)) and we simply
161 // can't pass them by-value as function arguments on MSVC.
162
163 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
164   template<std::size_t Size> \
165   struct AlignedCharArray<x, Size> { \
166     __declspec(align(x)) char buffer[Size]; \
167   };
168
169 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
170 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
171 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
172 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
173
174 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
175
176 #endif // _MSC_VER
177
178 namespace detail {
179 template <typename T1,
180           typename T2 = char, typename T3 = char, typename T4 = char,
181           typename T5 = char, typename T6 = char, typename T7 = char,
182           typename T8 = char, typename T9 = char, typename T10 = char>
183 class AlignerImpl {
184   T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7; T8 t8; T9 t9; T10 t10;
185
186   AlignerImpl(); // Never defined or instantiated.
187 };
188
189 template <typename T1,
190           typename T2 = char, typename T3 = char, typename T4 = char,
191           typename T5 = char, typename T6 = char, typename T7 = char,
192           typename T8 = char, typename T9 = char, typename T10 = char>
193 union SizerImpl {
194   char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
195        arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)],
196        arr9[sizeof(T9)], arr10[sizeof(T10)];
197 };
198 } // end namespace detail
199
200 /// \brief This union template exposes a suitably aligned and sized character
201 /// array member which can hold elements of any of up to ten types.
202 ///
203 /// These types may be arrays, structs, or any other types. The goal is to
204 /// expose a char array buffer member which can be used as suitable storage for
205 /// a placement new of any of these types. Support for more than ten types can
206 /// be added at the cost of more boilerplate.
207 template <typename T1,
208           typename T2 = char, typename T3 = char, typename T4 = char,
209           typename T5 = char, typename T6 = char, typename T7 = char,
210           typename T8 = char, typename T9 = char, typename T10 = char>
211 struct AlignedCharArrayUnion : llvm::AlignedCharArray<
212     AlignOf<detail::AlignerImpl<T1, T2, T3, T4, T5,
213                                 T6, T7, T8, T9, T10> >::Alignment,
214     sizeof(detail::SizerImpl<T1, T2, T3, T4, T5,
215                              T6, T7, T8, T9, T10>)> {
216 };
217 } // end namespace llvm
218 #endif