2007-10-17 22:56:47 +02:00
|
|
|
//===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-10-17 22:56:47 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the AlignOf function that computes alignments for
|
|
|
|
// arbitrary types.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_ALIGNOF_H
|
|
|
|
#define LLVM_SUPPORT_ALIGNOF_H
|
|
|
|
|
|
|
|
namespace llvm {
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-10-17 22:56:47 +02:00
|
|
|
template <typename T>
|
|
|
|
struct AlignmentCalcImpl {
|
|
|
|
char x;
|
|
|
|
T t;
|
|
|
|
private:
|
|
|
|
AlignmentCalcImpl() {} // Never instantiate.
|
|
|
|
};
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-10-17 22:56:47 +02:00
|
|
|
/// AlignOf - A templated class that contains an enum value representing
|
|
|
|
/// the alignment of the template argument. For example,
|
|
|
|
/// AlignOf<int>::Alignment represents the alignment of type "int". The
|
|
|
|
/// alignment calculated is the minimum alignment, and not necessarily
|
|
|
|
/// the "desired" alignment returned by GCC's __alignof__ (for example). Note
|
|
|
|
/// that because the alignment is an enum value, it can be used as a
|
|
|
|
/// compile-time constant (e.g., for template instantiation).
|
|
|
|
template <typename T>
|
|
|
|
struct AlignOf {
|
2008-05-05 20:30:58 +02:00
|
|
|
enum { Alignment =
|
|
|
|
static_cast<unsigned int>(sizeof(AlignmentCalcImpl<T>) - sizeof(T)) };
|
2008-04-28 19:58:20 +02:00
|
|
|
|
2008-02-11 18:24:50 +01:00
|
|
|
enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 };
|
|
|
|
enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 };
|
|
|
|
enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 };
|
|
|
|
enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 };
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2008-04-28 19:58:20 +02:00
|
|
|
enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 };
|
2009-02-20 23:51:36 +01:00
|
|
|
enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 };
|
2008-04-28 19:58:20 +02:00
|
|
|
enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 };
|
|
|
|
enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 };
|
|
|
|
|
2007-10-17 22:56:47 +02:00
|
|
|
};
|
2007-10-18 00:08:55 +02:00
|
|
|
|
2010-12-07 19:11:54 +01:00
|
|
|
/// alignOf - A templated function that returns the minimum alignment of
|
2007-10-18 00:08:55 +02:00
|
|
|
/// of a type. This provides no extra functionality beyond the AlignOf
|
|
|
|
/// class besides some cosmetic cleanliness. Example usage:
|
2010-10-30 07:14:01 +02:00
|
|
|
/// alignOf<int>() returns the alignment of an int.
|
2007-10-18 00:08:55 +02:00
|
|
|
template <typename T>
|
2010-10-30 07:14:01 +02:00
|
|
|
static inline unsigned alignOf() { return AlignOf<T>::Alignment; }
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-10-17 22:56:47 +02:00
|
|
|
} // end namespace llvm
|
|
|
|
#endif
|