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
21namespace llvm {
22
23template <typename T>
24struct AlignmentCalcImpl {
25  char x;
26  T t;
27private:
28  AlignmentCalcImpl() {} // Never instantiate.
29};
30
31/// AlignOf - A templated class that contains an enum value representing
32///  the alignment of the template argument.  For example,
33///  AlignOf<int>::Alignment represents the alignment of type "int".  The
34///  alignment calculated is the minimum alignment, and not necessarily
35///  the "desired" alignment returned by GCC's __alignof__ (for example).  Note
36///  that because the alignment is an enum value, it can be used as a
37///  compile-time constant (e.g., for template instantiation).
38template <typename T>
39struct AlignOf {
40  enum { Alignment =
41         static_cast<unsigned int>(sizeof(AlignmentCalcImpl<T>) - sizeof(T)) };
42
43  enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 };
44  enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 };
45  enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 };
46  enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 };
47
48  enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 };
49  enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 };
50  enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 };
51  enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 };
52
53};
54
55/// alignOf - A templated function that returns the minimum alignment of
56///  of a type.  This provides no extra functionality beyond the AlignOf
57///  class besides some cosmetic cleanliness.  Example usage:
58///  alignOf<int>() returns the alignment of an int.
59template <typename T>
60inline unsigned alignOf() { return AlignOf<T>::Alignment; }
61
62
63/// \brief Helper for building an aligned character array type.
64///
65/// This template is used to explicitly build up a collection of aligned
66/// character types. We have to build these up using a macro and explicit
67/// specialization to cope with old versions of MSVC and GCC where only an
68/// integer literal can be used to specify an alignment constraint. Once built
69/// up here, we can then begin to indirect between these using normal C++
70/// template parameters.
71template <size_t Alignment> struct AlignedCharArrayImpl;
72
73// MSVC requires special handling here.
74#ifndef _MSC_VER
75
76#if __has_feature(cxx_alignas)
77#define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
78  template <> struct AlignedCharArrayImpl<x> { \
79    char alignas(x) aligned; \
80  }
81#elif defined(__GNUC__)
82#define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
83  template <> struct AlignedCharArrayImpl<x> { \
84    char aligned __attribute__((aligned(x))); \
85  }
86#else
87# error No supported align as directive.
88#endif
89
90LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1);
91LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2);
92LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4);
93LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8);
94LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16);
95LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32);
96LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64);
97LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128);
98LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(512);
99LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1024);
100LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2048);
101LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4096);
102LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8192);
103
104#undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
105
106#else // _MSC_VER
107
108// We provide special variations of this template for the most common
109// alignments because __declspec(align(...)) doesn't actually work when it is
110// a member of a by-value function argument in MSVC, even if the alignment
111// request is something reasonably like 8-byte or 16-byte.
112template <> struct AlignedCharArrayImpl<1> { char aligned; };
113template <> struct AlignedCharArrayImpl<2> { short aligned; };
114template <> struct AlignedCharArrayImpl<4> { int aligned; };
115template <> struct AlignedCharArrayImpl<8> { double aligned; };
116
117#define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
118  template <> struct AlignedCharArrayImpl<x> { \
119    __declspec(align(x)) char aligned; \
120  }
121LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16);
122LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32);
123LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64);
124LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128);
125LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(512);
126LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1024);
127LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2048);
128LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4096);
129LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8192);
130// Any larger and MSVC complains.
131#undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
132
133#endif // _MSC_VER
134
135/// \brief This union template exposes a suitably aligned and sized character
136/// array member which can hold elements of any of up to four types.
137///
138/// These types may be arrays, structs, or any other types. The goal is to
139/// produce a union type containing a character array which, when used, forms
140/// storage suitable to placement new any of these types over. Support for more
141/// than four types can be added at the cost of more boiler plate.
142template <typename T1,
143          typename T2 = char, typename T3 = char, typename T4 = char>
144union AlignedCharArrayUnion {
145private:
146  class AlignerImpl {
147    T1 t1; T2 t2; T3 t3; T4 t4;
148
149    AlignerImpl(); // Never defined or instantiated.
150  };
151  union SizerImpl {
152    char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)];
153  };
154
155public:
156  /// \brief The character array buffer for use by clients.
157  ///
158  /// No other member of this union should be referenced. The exist purely to
159  /// constrain the layout of this character array.
160  char buffer[sizeof(SizerImpl)];
161
162private:
163  // Tests seem to indicate that both Clang and GCC will properly register the
164  // alignment of a struct containing an aligned member, and this alignment
165  // should carry over to the character array in the union.
166  llvm::AlignedCharArrayImpl<AlignOf<AlignerImpl>::Alignment> nonce_member;
167};
168
169} // end namespace llvm
170#endif
171