AlignOf.h revision a4d9869cf291e6f87bf9e7adfcd9a2c4e4518172
1//===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Ted Kremenek and is distributed under
6// the University of Illinois Open Source 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 <cassert>
19
20namespace llvm {
21
22template <typename T>
23struct AlignmentCalcImpl {
24  char x;
25  T t;
26private:
27  AlignmentCalcImpl() {} // Never instantiate.
28};
29
30/// AlignOf - A templated class that contains an enum value representing
31///  the alignment of the template argument.  For example,
32///  AlignOf<int>::Alignment represents the alignment of type "int".  The
33///  alignment calculated is the minimum alignment, and not necessarily
34///  the "desired" alignment returned by GCC's __alignof__ (for example).  Note
35///  that because the alignment is an enum value, it can be used as a
36///  compile-time constant (e.g., for template instantiation).
37template <typename T>
38struct AlignOf {
39  enum { Alignment = sizeof(AlignmentCalcImpl<T>) - sizeof(T) };
40};
41
42} // end namespace llvm
43#endif
44