f27ecdd7130ffd929846ed9b28a71e7ac10326f4
[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 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
20 namespace llvm {
21   
22 template <typename T>
23 struct AlignmentCalcImpl {
24   char x;
25   T t;
26 private:
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).
37 template <typename T>
38 struct AlignOf {
39   enum { Alignment = sizeof(AlignmentCalcImpl<T>) - sizeof(T) };
40 };
41   
42 } // end namespace llvm
43 #endif