LLVM API Documentation
00001 //===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the AlignOf function that computes alignments for 00011 // arbitrary types. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_SUPPORT_ALIGNOF_H 00016 #define LLVM_SUPPORT_ALIGNOF_H 00017 00018 namespace llvm { 00019 00020 template <typename T> 00021 struct AlignmentCalcImpl { 00022 char x; 00023 T t; 00024 private: 00025 AlignmentCalcImpl() {} // Never instantiate. 00026 }; 00027 00028 /// AlignOf - A templated class that contains an enum value representing 00029 /// the alignment of the template argument. For example, 00030 /// AlignOf<int>::Alignment represents the alignment of type "int". The 00031 /// alignment calculated is the minimum alignment, and not necessarily 00032 /// the "desired" alignment returned by GCC's __alignof__ (for example). Note 00033 /// that because the alignment is an enum value, it can be used as a 00034 /// compile-time constant (e.g., for template instantiation). 00035 template <typename T> 00036 struct AlignOf { 00037 enum { Alignment = 00038 static_cast<unsigned int>(sizeof(AlignmentCalcImpl<T>) - sizeof(T)) }; 00039 00040 enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 }; 00041 enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 }; 00042 enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 }; 00043 enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 }; 00044 00045 enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 }; 00046 enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 }; 00047 enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 }; 00048 enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 }; 00049 00050 }; 00051 00052 /// alignof - A templated function that returns the mininum alignment of 00053 /// of a type. This provides no extra functionality beyond the AlignOf 00054 /// class besides some cosmetic cleanliness. Example usage: 00055 /// alignof<int>() returns the alignment of an int. 00056 template <typename T> 00057 static inline unsigned alignof() { return AlignOf<T>::Alignment; } 00058 00059 } // end namespace llvm 00060 #endif