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 /// It should be noted that characters and bytes are distinct concepts. Bytes
30 /// refer to addressable units of data storage on the target machine, and
31 /// characters are members of a set of elements used for the organization,
32 /// control, or representation of data. According to C99, bytes are allowed
33 /// to exceed characters in size, although currently, clang only supports
34 /// architectures where the two are the same size.
36 /// For portability, never assume that a target character is 8 bits wide. Use
37 /// CharUnit values whereever you calculate sizes, offsets, or alignments
38 /// in character units.
41 typedef int64_t QuantityType;
44 QuantityType Quantity;
46 explicit CharUnits(QuantityType C) : Quantity(C) {}
50 /// CharUnits - A default constructor.
51 CharUnits() : Quantity(0) {}
53 /// Zero - Construct a CharUnits quantity of zero.
54 static CharUnits Zero() {
58 /// One - Construct a CharUnits quantity of one.
59 static CharUnits One() {
63 /// fromQuantity - Construct a CharUnits quantity from a raw integer type.
64 static CharUnits fromQuantity(QuantityType Quantity) {
65 return CharUnits(Quantity);
68 // Compound assignment.
69 CharUnits& operator+= (const CharUnits &Other) {
70 Quantity += Other.Quantity;
73 CharUnits& operator-= (const CharUnits &Other) {
74 Quantity -= Other.Quantity;
78 // Comparison operators.
79 bool operator== (const CharUnits &Other) const {
80 return Quantity == Other.Quantity;
82 bool operator!= (const CharUnits &Other) const {
83 return Quantity != Other.Quantity;
86 // Relational operators.
87 bool operator< (const CharUnits &Other) const {
88 return Quantity < Other.Quantity;
90 bool operator<= (const CharUnits &Other) const {
91 return Quantity <= Other.Quantity;
93 bool operator> (const CharUnits &Other) const {
94 return Quantity > Other.Quantity;
96 bool operator>= (const CharUnits &Other) const {
97 return Quantity >= Other.Quantity;
102 /// isZero - Test whether the quantity equals zero.
103 bool isZero() const { return Quantity == 0; }
105 /// isOne - Test whether the quantity equals one.
106 bool isOne() const { return Quantity == 1; }
108 /// isPositive - Test whether the quantity is greater than zero.
109 bool isPositive() const { return Quantity > 0; }
111 /// isNegative - Test whether the quantity is less than zero.
112 bool isNegative() const { return Quantity < 0; }
114 // Arithmetic operators.
115 CharUnits operator* (QuantityType N) const {
116 return CharUnits(Quantity * N);
118 CharUnits operator/ (QuantityType N) const {
119 return CharUnits(Quantity / N);
121 QuantityType operator/ (const CharUnits &Other) const {
122 return Quantity / Other.Quantity;
124 CharUnits operator% (QuantityType N) const {
125 return CharUnits(Quantity % N);
127 QuantityType operator% (const CharUnits &Other) const {
128 return Quantity % Other.Quantity;
130 CharUnits operator+ (const CharUnits &Other) const {
131 return CharUnits(Quantity + Other.Quantity);
133 CharUnits operator- (const CharUnits &Other) const {
134 return CharUnits(Quantity - Other.Quantity);
136 CharUnits operator- () const {
137 return CharUnits(-Quantity);
143 /// getQuantity - Get the raw integer representation of this quantity.
144 QuantityType getQuantity() const { return Quantity; }
146 /// RoundUpToAlignment - Returns the next integer (mod 2**64) that is
147 /// greater than or equal to this quantity and is a multiple of \arg
148 /// Align. Align must be non-zero.
149 CharUnits RoundUpToAlignment(const CharUnits &Align) {
150 return CharUnits(llvm::RoundUpToAlignment(Quantity,
158 inline clang::CharUnits operator* (clang::CharUnits::QuantityType Scale,
159 const clang::CharUnits &CU) {
165 template<> struct DenseMapInfo<clang::CharUnits> {
166 static clang::CharUnits getEmptyKey() {
167 clang::CharUnits::QuantityType Quantity =
168 DenseMapInfo<clang::CharUnits::QuantityType>::getEmptyKey();
170 return clang::CharUnits::fromQuantity(Quantity);
173 static clang::CharUnits getTombstoneKey() {
174 clang::CharUnits::QuantityType Quantity =
175 DenseMapInfo<clang::CharUnits::QuantityType>::getTombstoneKey();
177 return clang::CharUnits::fromQuantity(Quantity);
180 static unsigned getHashValue(const clang::CharUnits &CU) {
181 clang::CharUnits::QuantityType Quantity = CU.getQuantity();
182 return DenseMapInfo<clang::CharUnits::QuantityType>::getHashValue(Quantity);
185 static bool isEqual(const clang::CharUnits &LHS,
186 const clang::CharUnits &RHS) {
191 template <> struct isPodLike<clang::CharUnits> {
192 static const bool value = true;
195 } // end namespace llvm
197 #endif // LLVM_CLANG_AST_CHARUNITS_H