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 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 00056 template <typename T> 00057 static inline unsigned alignOf() { return AlignOf<T>::Alignment; } 00058 00059 } // end namespace llvm 00060 #endif