File indexing completed on 2026-05-10 08:36:29
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_CLANG_AST_CHARUNITS_H
0014 #define LLVM_CLANG_AST_CHARUNITS_H
0015
0016 #include "llvm/ADT/DenseMapInfo.h"
0017 #include "llvm/Support/Alignment.h"
0018 #include "llvm/Support/DataTypes.h"
0019 #include "llvm/Support/MathExtras.h"
0020
0021 namespace clang {
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 class CharUnits {
0039 public:
0040 typedef int64_t QuantityType;
0041
0042 private:
0043 QuantityType Quantity = 0;
0044
0045 explicit CharUnits(QuantityType C) : Quantity(C) {}
0046
0047 public:
0048
0049
0050 CharUnits() = default;
0051
0052
0053 static CharUnits Zero() {
0054 return CharUnits(0);
0055 }
0056
0057
0058 static CharUnits One() {
0059 return CharUnits(1);
0060 }
0061
0062
0063 static CharUnits fromQuantity(QuantityType Quantity) {
0064 return CharUnits(Quantity);
0065 }
0066
0067
0068
0069 static CharUnits fromQuantity(llvm::Align Quantity) {
0070 return CharUnits(Quantity.value());
0071 }
0072
0073
0074 CharUnits& operator+= (const CharUnits &Other) {
0075 Quantity += Other.Quantity;
0076 return *this;
0077 }
0078 CharUnits& operator++ () {
0079 ++Quantity;
0080 return *this;
0081 }
0082 CharUnits operator++ (int) {
0083 return CharUnits(Quantity++);
0084 }
0085 CharUnits& operator-= (const CharUnits &Other) {
0086 Quantity -= Other.Quantity;
0087 return *this;
0088 }
0089 CharUnits& operator-- () {
0090 --Quantity;
0091 return *this;
0092 }
0093 CharUnits operator-- (int) {
0094 return CharUnits(Quantity--);
0095 }
0096
0097
0098 bool operator== (const CharUnits &Other) const {
0099 return Quantity == Other.Quantity;
0100 }
0101 bool operator!= (const CharUnits &Other) const {
0102 return Quantity != Other.Quantity;
0103 }
0104
0105
0106 bool operator< (const CharUnits &Other) const {
0107 return Quantity < Other.Quantity;
0108 }
0109 bool operator<= (const CharUnits &Other) const {
0110 return Quantity <= Other.Quantity;
0111 }
0112 bool operator> (const CharUnits &Other) const {
0113 return Quantity > Other.Quantity;
0114 }
0115 bool operator>= (const CharUnits &Other) const {
0116 return Quantity >= Other.Quantity;
0117 }
0118
0119
0120
0121
0122 bool isZero() const { return Quantity == 0; }
0123
0124
0125 bool isOne() const { return Quantity == 1; }
0126
0127
0128 bool isPositive() const { return Quantity > 0; }
0129
0130
0131 bool isNegative() const { return Quantity < 0; }
0132
0133
0134
0135 bool isPowerOfTwo() const {
0136 return (Quantity & -Quantity) == Quantity;
0137 }
0138
0139
0140
0141
0142
0143 bool isMultipleOf(CharUnits N) const {
0144 return (*this % N) == 0;
0145 }
0146
0147
0148 CharUnits operator* (QuantityType N) const {
0149 return CharUnits(Quantity * N);
0150 }
0151 CharUnits &operator*= (QuantityType N) {
0152 Quantity *= N;
0153 return *this;
0154 }
0155 CharUnits operator/ (QuantityType N) const {
0156 return CharUnits(Quantity / N);
0157 }
0158 CharUnits &operator/= (QuantityType N) {
0159 Quantity /= N;
0160 return *this;
0161 }
0162 QuantityType operator/ (const CharUnits &Other) const {
0163 return Quantity / Other.Quantity;
0164 }
0165 CharUnits operator% (QuantityType N) const {
0166 return CharUnits(Quantity % N);
0167 }
0168 QuantityType operator% (const CharUnits &Other) const {
0169 return Quantity % Other.Quantity;
0170 }
0171 CharUnits operator+ (const CharUnits &Other) const {
0172 return CharUnits(Quantity + Other.Quantity);
0173 }
0174 CharUnits operator- (const CharUnits &Other) const {
0175 return CharUnits(Quantity - Other.Quantity);
0176 }
0177 CharUnits operator- () const {
0178 return CharUnits(-Quantity);
0179 }
0180
0181
0182
0183
0184
0185 QuantityType getQuantity() const { return Quantity; }
0186
0187
0188
0189 llvm::Align getAsAlign() const { return llvm::Align(Quantity); }
0190
0191
0192
0193
0194 llvm::MaybeAlign getAsMaybeAlign() const {
0195 return llvm::MaybeAlign(Quantity);
0196 }
0197
0198
0199
0200
0201 CharUnits alignTo(const CharUnits &Align) const {
0202 return CharUnits(llvm::alignTo(Quantity, Align.Quantity));
0203 }
0204
0205
0206
0207 CharUnits alignmentAtOffset(CharUnits offset) const {
0208 assert(Quantity != 0 && "offsetting from unknown alignment?");
0209 return CharUnits(llvm::MinAlign(Quantity, offset.Quantity));
0210 }
0211
0212
0213
0214 CharUnits alignmentOfArrayElement(CharUnits elementSize) const {
0215
0216
0217
0218 return alignmentAtOffset(elementSize);
0219 }
0220
0221
0222 };
0223 }
0224
0225 inline clang::CharUnits operator* (clang::CharUnits::QuantityType Scale,
0226 const clang::CharUnits &CU) {
0227 return CU * Scale;
0228 }
0229
0230 namespace llvm {
0231
0232 template<> struct DenseMapInfo<clang::CharUnits> {
0233 static clang::CharUnits getEmptyKey() {
0234 clang::CharUnits::QuantityType Quantity =
0235 DenseMapInfo<clang::CharUnits::QuantityType>::getEmptyKey();
0236
0237 return clang::CharUnits::fromQuantity(Quantity);
0238 }
0239
0240 static clang::CharUnits getTombstoneKey() {
0241 clang::CharUnits::QuantityType Quantity =
0242 DenseMapInfo<clang::CharUnits::QuantityType>::getTombstoneKey();
0243
0244 return clang::CharUnits::fromQuantity(Quantity);
0245 }
0246
0247 static unsigned getHashValue(const clang::CharUnits &CU) {
0248 clang::CharUnits::QuantityType Quantity = CU.getQuantity();
0249 return DenseMapInfo<clang::CharUnits::QuantityType>::getHashValue(Quantity);
0250 }
0251
0252 static bool isEqual(const clang::CharUnits &LHS,
0253 const clang::CharUnits &RHS) {
0254 return LHS == RHS;
0255 }
0256 };
0257
0258 }
0259
0260 #endif