fixing stuff
[dyninst.git] / dynutil / h / dyn_detail / boost / type_traits / type_with_alignment.hpp
1 //  (C) Copyright John Maddock 2000.
2 //  Use, modification and distribution are subject to the Boost Software License,
3 //  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
4 //  http://www.boost.org/LICENSE_1_0.txt).
5 //
6 //  See http://www.boost.org/libs/type_traits for most recent version including documentation.
7
8 #ifndef DYN_DETAIL_BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
9 #define DYN_DETAIL_BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
10
11 #include <dyn_detail/boost/mpl/if.hpp>
12 #include <dyn_detail/boost/preprocessor/list/for_each_i.hpp>
13 #include <dyn_detail/boost/preprocessor/tuple/to_list.hpp>
14 #include <dyn_detail/boost/preprocessor/cat.hpp>
15 #include <dyn_detail/boost/preprocessor/list/transform.hpp>
16 #include <dyn_detail/boost/preprocessor/list/append.hpp>
17 #include <dyn_detail/boost/type_traits/alignment_of.hpp>
18 #include <dyn_detail/boost/type_traits/is_pod.hpp>
19 #include <dyn_detail/boost/static_assert.hpp>
20 #include <dyn_detail/boost/config.hpp>
21
22 // should be the last #include
23 #include <dyn_detail/boost/type_traits/detail/bool_trait_def.hpp>
24
25 #include <cstddef>
26
27 #ifdef DYN_DETAIL_BOOST_MSVC
28 #   pragma warning(push)
29 #   pragma warning(disable: 4121) // alignment is sensitive to packing
30 #endif
31
32 namespace dyn_detail {
33 namespace boost {
34
35 #ifndef __BORLANDC__
36
37 namespace detail {
38
39 class alignment_dummy;
40 typedef void (*function_ptr)();
41 typedef int (alignment_dummy::*member_ptr);
42 typedef int (alignment_dummy::*member_function_ptr)();
43
44 #ifdef DYN_DETAIL_BOOST_HAS_LONG_LONG
45 #define DYN_DETAIL_BOOST_TT_ALIGNMENT_BASE_TYPES DYN_DETAIL_BOOST_PP_TUPLE_TO_LIST( \
46         12, ( \
47         char, short, int, long,  ::dyn_detail::boost::long_long_type, float, double, long double \
48         , void*, function_ptr, member_ptr, member_function_ptr))
49 #else
50 #define DYN_DETAIL_BOOST_TT_ALIGNMENT_BASE_TYPES DYN_DETAIL_BOOST_PP_TUPLE_TO_LIST( \
51         11, ( \
52         char, short, int, long, float, double, long double \
53         , void*, function_ptr, member_ptr, member_function_ptr))
54 #endif
55
56 #define DYN_DETAIL_BOOST_TT_HAS_ONE_T(D,Data,T) ::dyn_detail::boost::detail::has_one_T< T >
57
58 #define DYN_DETAIL_BOOST_TT_ALIGNMENT_STRUCT_TYPES                         \
59         DYN_DETAIL_BOOST_PP_LIST_TRANSFORM(DYN_DETAIL_BOOST_TT_HAS_ONE_T,             \
60                                 X,                              \
61                                 DYN_DETAIL_BOOST_TT_ALIGNMENT_BASE_TYPES)
62
63 #define DYN_DETAIL_BOOST_TT_ALIGNMENT_TYPES                                \
64         DYN_DETAIL_BOOST_PP_LIST_APPEND(DYN_DETAIL_BOOST_TT_ALIGNMENT_BASE_TYPES,     \
65                              DYN_DETAIL_BOOST_TT_ALIGNMENT_STRUCT_TYPES)
66
67 //
68 // lower_alignment_helper --
69 //
70 // This template gets instantiated a lot, so use partial
71 // specialization when available to reduce the compiler burden.
72 //
73 #ifdef DYN_DETAIL_BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
74 template <bool found = true>
75 struct lower_alignment_helper_impl
76 {
77     template <std::size_t, class>
78     struct apply
79     {
80         typedef char type;
81         enum { value = true };
82     };
83 };
84
85 template <>
86 struct lower_alignment_helper_impl<false>
87 {
88     template <std::size_t target, class TestType>
89     struct apply
90       : mpl::if_c<(alignment_of<TestType>::value == target), TestType, char>
91     {
92         enum { value = (alignment_of<TestType>::value == target) };
93     };
94 };
95
96 template <bool found, std::size_t target, class TestType>
97 struct lower_alignment_helper
98   : lower_alignment_helper_impl<found>::template apply<target,TestType>
99 {
100 };
101 #else
102 template <bool found, std::size_t target, class TestType>
103 struct lower_alignment_helper
104 {
105     typedef char type;
106     enum { value = true };
107 };
108
109 template <std::size_t target, class TestType>
110 struct lower_alignment_helper<false,target,TestType>
111 {
112     enum { value = (alignment_of<TestType>::value == target) };
113     typedef typename mpl::if_c<value, TestType, char>::type type;
114 };
115 #endif
116
117 #define DYN_DETAIL_BOOST_TT_CHOOSE_MIN_ALIGNMENT(R,P,I,T)                                  \
118         typename lower_alignment_helper<                                        \
119           DYN_DETAIL_BOOST_PP_CAT(found,I),target,T                                        \
120         >::type DYN_DETAIL_BOOST_PP_CAT(t,I);                                              \
121         enum {                                                                  \
122             DYN_DETAIL_BOOST_PP_CAT(found,DYN_DETAIL_BOOST_PP_INC(I))                                 \
123               = lower_alignment_helper<DYN_DETAIL_BOOST_PP_CAT(found,I),target,T >::value  \
124         };
125
126 #define DYN_DETAIL_BOOST_TT_CHOOSE_T(R,P,I,T) T DYN_DETAIL_BOOST_PP_CAT(t,I);
127
128 template <typename T>
129 struct has_one_T
130 {
131   T data;
132 };
133
134 template <std::size_t target>
135 union lower_alignment
136 {
137     enum { found0 = false };
138
139     DYN_DETAIL_BOOST_PP_LIST_FOR_EACH_I(
140           DYN_DETAIL_BOOST_TT_CHOOSE_MIN_ALIGNMENT
141         , ignored
142         , DYN_DETAIL_BOOST_TT_ALIGNMENT_TYPES
143         )
144 };
145
146 union max_align
147 {
148     DYN_DETAIL_BOOST_PP_LIST_FOR_EACH_I(
149           DYN_DETAIL_BOOST_TT_CHOOSE_T
150         , ignored
151         , DYN_DETAIL_BOOST_TT_ALIGNMENT_TYPES
152         )
153 };
154
155 #undef DYN_DETAIL_BOOST_TT_ALIGNMENT_BASE_TYPES
156 #undef DYN_DETAIL_BOOST_TT_HAS_ONE_T
157 #undef DYN_DETAIL_BOOST_TT_ALIGNMENT_STRUCT_TYPES
158 #undef DYN_DETAIL_BOOST_TT_ALIGNMENT_TYPES
159 #undef DYN_DETAIL_BOOST_TT_CHOOSE_MIN_ALIGNMENT
160 #undef DYN_DETAIL_BOOST_TT_CHOOSE_T
161
162 template<std::size_t TAlign, std::size_t Align>
163 struct is_aligned
164 {
165     DYN_DETAIL_BOOST_STATIC_CONSTANT(bool,
166         value = (TAlign >= Align) & (TAlign % Align == 0)
167         );
168 };
169
170 #ifdef DYN_DETAIL_BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
171 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::detail::max_align,true)
172 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::detail::lower_alignment<1> ,true)
173 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::detail::lower_alignment<2> ,true)
174 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::detail::lower_alignment<4> ,true)
175 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::detail::lower_alignment<8> ,true)
176 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::detail::lower_alignment<10> ,true)
177 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::detail::lower_alignment<16> ,true)
178 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::detail::lower_alignment<32> ,true)
179 #endif
180
181 } // namespace detail
182
183 #ifndef DYN_DETAIL_BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
184 template<std::size_t Align>
185 struct is_pod< ::dyn_detail::boost::detail::lower_alignment<Align> >
186 {
187         DYN_DETAIL_BOOST_STATIC_CONSTANT(std::size_t, value = true);
188 };
189 #endif
190
191 // This alignment method originally due to Brian Parker, implemented by David
192 // Abrahams, and then ported here by Doug Gregor.
193 namespace detail{
194
195 template <std::size_t Align>
196 class type_with_alignment_imp
197 {
198         typedef ::dyn_detail::boost::detail::lower_alignment<Align> t1;
199     typedef typename mpl::if_c<
200           ::dyn_detail::boost::detail::is_aligned< ::dyn_detail::boost::alignment_of<t1>::value,Align >::value
201         , t1
202         , ::dyn_detail::boost::detail::max_align
203         >::type align_t;
204
205     DYN_DETAIL_BOOST_STATIC_CONSTANT(std::size_t, found = alignment_of<align_t>::value);
206
207     DYN_DETAIL_BOOST_STATIC_ASSERT(found >= Align);
208     DYN_DETAIL_BOOST_STATIC_ASSERT(found % Align == 0);
209
210  public:
211     typedef align_t type;
212 };
213
214 }
215
216 template <std::size_t Align>
217 class type_with_alignment 
218   : public ::dyn_detail::boost::detail::type_with_alignment_imp<Align>
219 {
220 };
221
222 #if defined(__GNUC__)
223 namespace align {
224 struct __attribute__((__aligned__(2))) a2 {};
225 struct __attribute__((__aligned__(4))) a4 {};
226 struct __attribute__((__aligned__(8))) a8 {};
227 struct __attribute__((__aligned__(16))) a16 {};
228 struct __attribute__((__aligned__(32))) a32 {};
229 }
230
231 template<> class type_with_alignment<1>  { public: typedef char type; };
232 template<> class type_with_alignment<2>  { public: typedef align::a2 type; };
233 template<> class type_with_alignment<4>  { public: typedef align::a4 type; };
234 template<> class type_with_alignment<8>  { public: typedef align::a8 type; };
235 template<> class type_with_alignment<16> { public: typedef align::a16 type; };
236 template<> class type_with_alignment<32> { public: typedef align::a32 type; };
237
238 namespace detail {
239 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a2,true)
240 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a4,true)
241 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a8,true)
242 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a16,true)
243 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a32,true)
244 }
245 #endif
246 #if (defined(DYN_DETAIL_BOOST_MSVC) || (defined(DYN_DETAIL_BOOST_INTEL) && defined(_MSC_VER))) && _MSC_VER >= 1300
247 //
248 // MSVC supports types which have alignments greater than the normal
249 // maximum: these are used for example in the types __m64 and __m128
250 // to provide types with alignment requirements which match the SSE
251 // registers.  Therefore we extend type_with_alignment<> to support
252 // such types, however, we have to be careful to use a builtin type
253 // whenever possible otherwise we break previously working code:
254 // see http://article.gmane.org/gmane.comp.lib.boost.devel/173011
255 // for an example and test case.  Thus types like a8 below will
256 // be used *only* if the existing implementation can't provide a type
257 // with suitable alignment.  This does mean however, that type_with_alignment<>
258 // may return a type which cannot be passed through a function call
259 // by value (and neither can any type containing such a type like
260 // Boost.Optional).  However, this only happens when we have no choice 
261 // in the matter because no other "ordinary" type is available.
262 //
263 namespace align {
264 struct __declspec(align(8)) a8 { 
265    char m[8]; 
266    typedef a8 type;
267 };
268 struct __declspec(align(16)) a16 { 
269    char m[16]; 
270    typedef a16 type;
271 };
272 struct __declspec(align(32)) a32 { 
273    char m[32]; 
274    typedef a32 type;
275 };
276 struct __declspec(align(64)) a64 
277
278    char m[64]; 
279    typedef a64 type;
280 };
281 struct __declspec(align(128)) a128 { 
282    char m[128]; 
283    typedef a128 type;
284 };
285 }
286
287 template<> class type_with_alignment<8>  
288
289    typedef mpl::if_c<
290       ::dyn_detail::boost::alignment_of<detail::max_align>::value < 8,
291       align::a8,
292       detail::type_with_alignment_imp<8> >::type t1; 
293 public: 
294    typedef t1::type type;
295 };
296 template<> class type_with_alignment<16> 
297
298    typedef mpl::if_c<
299       ::dyn_detail::boost::alignment_of<detail::max_align>::value < 16,
300       align::a16,
301       detail::type_with_alignment_imp<16> >::type t1; 
302 public: 
303    typedef t1::type type;
304 };
305 template<> class type_with_alignment<32> 
306
307    typedef mpl::if_c<
308       ::dyn_detail::boost::alignment_of<detail::max_align>::value < 32,
309       align::a32,
310       detail::type_with_alignment_imp<32> >::type t1; 
311 public: 
312    typedef t1::type type;
313 };
314 template<> class type_with_alignment<64> {
315    typedef mpl::if_c<
316       ::dyn_detail::boost::alignment_of<detail::max_align>::value < 64,
317       align::a64,
318       detail::type_with_alignment_imp<64> >::type t1; 
319 public: 
320    typedef t1::type type;
321 };
322 template<> class type_with_alignment<128> {
323    typedef mpl::if_c<
324       ::dyn_detail::boost::alignment_of<detail::max_align>::value < 128,
325       align::a128,
326       detail::type_with_alignment_imp<128> >::type t1; 
327 public: 
328    typedef t1::type type;
329 };
330
331 namespace detail {
332 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a8,true)
333 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a16,true)
334 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a32,true)
335 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a64,true)
336 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a128,true)
337 }
338 #endif
339
340 #else
341
342 //
343 // Borland specific version, we have this for two reasons:
344 // 1) The version above doesn't always compile (with the new test cases for example)
345 // 2) Because of Borlands #pragma option we can create types with alignments that are
346 //    greater that the largest aligned builtin type.
347
348 namespace align{
349 #pragma option push -a16
350 struct a2{ short s; };
351 struct a4{ int s; };
352 struct a8{ double s; };
353 struct a16{ long double s; };
354 #pragma option pop
355 }
356
357 namespace detail {
358
359         typedef ::dyn_detail::boost::align::a16 max_align;
360
361 //#if ! DYN_DETAIL_BOOST_WORKAROUND(__CODEGEARC__, DYN_DETAIL_BOOST_TESTED_AT(0x610))
362 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a2,true)
363 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a4,true)
364 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a8,true)
365 DYN_DETAIL_BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::dyn_detail::boost::align::a16,true)
366 //#endif
367 }
368
369 template <std::size_t N> struct type_with_alignment
370 {
371    // We should never get to here, but if we do use the maximally
372    // aligned type:
373    // DYN_DETAIL_BOOST_STATIC_ASSERT(0);
374    typedef align::a16 type;
375 };
376 template <> struct type_with_alignment<1>{ typedef char type; };
377 template <> struct type_with_alignment<2>{ typedef align::a2 type; };
378 template <> struct type_with_alignment<4>{ typedef align::a4 type; };
379 template <> struct type_with_alignment<8>{ typedef align::a8 type; };
380 template <> struct type_with_alignment<16>{ typedef align::a16 type; };
381
382 #endif
383
384 } // namespace boost
385 } // namespace dyn_detail
386
387 #ifdef DYN_DETAIL_BOOST_MSVC
388 #   pragma warning(pop)
389 #endif
390
391 #include <dyn_detail/boost/type_traits/detail/bool_trait_undef.hpp>
392
393 #endif // DYN_DETAIL_BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
394
395