1 //===--- CharUnits.h - Character units for sizes and offsets ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the CharUnits class
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_CHARUNITS_H
15 #define LLVM_CLANG_AST_CHARUNITS_H
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/Support/DataTypes.h"
19 #include "llvm/Support/MathExtras.h"
23 /// CharUnits - This is an opaque type for sizes expressed in character units.
24 /// Instances of this type represent a quantity as a multiple of the size
25 /// of the standard C type, char, on the target architecture. As an opaque
26 /// type, CharUnits protects you from accidentally combining operations on
27 /// quantities in bit units and character units.
29 /// In both C and C++, an object of type 'char', 'signed char', or 'unsigned
30 /// char' occupies exactly one byte, so 'character unit' and 'byte' refer to
31 /// the same quantity of storage. However, we use the term 'character unit'
32 /// rather than 'byte' to avoid an implication that a character unit is
35 /// For portability, never assume that a target character is 8 bits wide. Use
36 /// CharUnit values wherever you calculate sizes, offsets, or alignments
37 /// in character units.
40 typedef int64_t QuantityType;
43 QuantityType Quantity;
45 explicit CharUnits(QuantityType C) : Quantity(C) {}
49 /// CharUnits - A default constructor.
50 CharUnits() : Quantity(0) {}
52 /// Zero - Construct a CharUnits quantity of zero.
53 static CharUnits Zero() {
57 /// One - Construct a CharUnits quantity of one.
58 static CharUnits One() {
62 /// fromQuantity - Construct a CharUnits quantity from a raw integer type.
63 static CharUnits fromQuantity(QuantityType Quantity) {
64 return CharUnits(Quantity);
67 // Compound assignment.
68 CharUnits& operator+= (const CharUnits &Other) {
69 Quantity += Other.Quantity;
72 CharUnits& operator++ () {
76 CharUnits operator++ (int) {
77 return CharUnits(Quantity++);
79 CharUnits& operator-= (const CharUnits &Other) {
80 Quantity -= Other.Quantity;
83 CharUnits& operator-- () {
87 CharUnits operator-- (int) {
88 return CharUnits(Quantity--);
91 // Comparison operators.
92 bool operator== (const CharUnits &Other) const {
93 return Quantity == Other.Quantity;
95 bool operator!= (const CharUnits &Other) const {
96 return Quantity != Other.Quantity;
99 // Relational operators.
100 bool operator< (const CharUnits &Other) const {
101 return Quantity < Other.Quantity;
103 bool operator<= (const CharUnits &Other) const {
104 return Quantity <= Other.Quantity;
106 bool operator> (const CharUnits &Other) const {
107 return Quantity > Other.Quantity;
109 bool operator>= (const CharUnits &Other) const {
110 return Quantity >= Other.Quantity;
115 /// isZero - Test whether the quantity equals zero.
116 bool isZero() const { return Quantity == 0; }
118 /// isOne - Test whether the quantity equals one.
119 bool isOne() const { return Quantity == 1; }
121 /// isPositive - Test whether the quantity is greater than zero.
122 bool isPositive() const { return Quantity > 0; }
124 /// isNegative - Test whether the quantity is less than zero.
125 bool isNegative() const { return Quantity < 0; }
127 /// isPowerOfTwo - Test whether the quantity is a power of two.
128 /// Zero is not a power of two.
129 bool isPowerOfTwo() const {
130 return (Quantity & -Quantity) == Quantity;
133 /// Test whether this is a multiple of the other value.
135 /// Among other things, this promises that
136 /// self.alignTo(N) will just return self.
137 bool isMultipleOf(CharUnits N) const {
138 return (*this % N) == 0;
141 // Arithmetic operators.
142 CharUnits operator* (QuantityType N) const {
143 return CharUnits(Quantity * N);
145 CharUnits &operator*= (QuantityType N) {
149 CharUnits operator/ (QuantityType N) const {
150 return CharUnits(Quantity / N);
152 CharUnits &operator/= (QuantityType N) {
156 QuantityType operator/ (const CharUnits &Other) const {
157 return Quantity / Other.Quantity;
159 CharUnits operator% (QuantityType N) const {
160 return CharUnits(Quantity % N);
162 QuantityType operator% (const CharUnits &Other) const {
163 return Quantity % Other.Quantity;
165 CharUnits operator+ (const CharUnits &Other) const {
166 return CharUnits(Quantity + Other.Quantity);
168 CharUnits operator- (const CharUnits &Other) const {
169 return CharUnits(Quantity - Other.Quantity);
171 CharUnits operator- () const {
172 return CharUnits(-Quantity);
178 /// getQuantity - Get the raw integer representation of this quantity.
179 QuantityType getQuantity() const { return Quantity; }
181 /// alignTo - Returns the next integer (mod 2**64) that is
182 /// greater than or equal to this quantity and is a multiple of \p Align.
183 /// Align must be non-zero.
184 CharUnits alignTo(const CharUnits &Align) const {
185 return CharUnits(llvm::alignTo(Quantity, Align.Quantity));
188 /// Given that this is a non-zero alignment value, what is the
189 /// alignment at the given offset?
190 CharUnits alignmentAtOffset(CharUnits offset) const {
191 assert(Quantity != 0 && "offsetting from unknown alignment?");
192 return CharUnits(llvm::MinAlign(Quantity, offset.Quantity));
195 /// Given that this is the alignment of the first element of an
196 /// array, return the minimum alignment of any element in the array.
197 CharUnits alignmentOfArrayElement(CharUnits elementSize) const {
198 // Since we don't track offsetted alignments, the alignment of
199 // the second element (or any odd element) will be minimally
201 return alignmentAtOffset(elementSize);
208 inline clang::CharUnits operator* (clang::CharUnits::QuantityType Scale,
209 const clang::CharUnits &CU) {
215 template<> struct DenseMapInfo<clang::CharUnits> {
216 static clang::CharUnits getEmptyKey() {
217 clang::CharUnits::QuantityType Quantity =
218 DenseMapInfo<clang::CharUnits::QuantityType>::getEmptyKey();
220 return clang::CharUnits::fromQuantity(Quantity);
223 static clang::CharUnits getTombstoneKey() {
224 clang::CharUnits::QuantityType Quantity =
225 DenseMapInfo<clang::CharUnits::QuantityType>::getTombstoneKey();
227 return clang::CharUnits::fromQuantity(Quantity);
230 static unsigned getHashValue(const clang::CharUnits &CU) {
231 clang::CharUnits::QuantityType Quantity = CU.getQuantity();
232 return DenseMapInfo<clang::CharUnits::QuantityType>::getHashValue(Quantity);
235 static bool isEqual(const clang::CharUnits &LHS,
236 const clang::CharUnits &RHS) {
241 template <> struct isPodLike<clang::CharUnits> {
242 static const bool value = true;
245 } // end namespace llvm
247 #endif // LLVM_CLANG_AST_CHARUNITS_H