/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/AST/CharUnits.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- CharUnits.h - Character units for sizes and offsets ----*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines the CharUnits class |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #ifndef LLVM_CLANG_AST_CHARUNITS_H |
14 | | #define LLVM_CLANG_AST_CHARUNITS_H |
15 | | |
16 | | #include "llvm/ADT/DenseMapInfo.h" |
17 | | #include "llvm/Support/Alignment.h" |
18 | | #include "llvm/Support/DataTypes.h" |
19 | | #include "llvm/Support/MathExtras.h" |
20 | | |
21 | | namespace clang { |
22 | | |
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. |
28 | | /// |
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 |
33 | | /// exactly 8 bits. |
34 | | /// |
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. |
38 | | class CharUnits { |
39 | | public: |
40 | | typedef int64_t QuantityType; |
41 | | |
42 | | private: |
43 | | QuantityType Quantity = 0; |
44 | | |
45 | 73.5M | explicit CharUnits(QuantityType C) : Quantity(C) {} |
46 | | |
47 | | public: |
48 | | |
49 | | /// CharUnits - A default constructor. |
50 | 24.5M | CharUnits() = default; |
51 | | |
52 | | /// Zero - Construct a CharUnits quantity of zero. |
53 | 12.9M | static CharUnits Zero() { |
54 | 12.9M | return CharUnits(0); |
55 | 12.9M | } |
56 | | |
57 | | /// One - Construct a CharUnits quantity of one. |
58 | 3.29M | static CharUnits One() { |
59 | 3.29M | return CharUnits(1); |
60 | 3.29M | } |
61 | | |
62 | | /// fromQuantity - Construct a CharUnits quantity from a raw integer type. |
63 | 49.6M | static CharUnits fromQuantity(QuantityType Quantity) { |
64 | 49.6M | return CharUnits(Quantity); |
65 | 49.6M | } |
66 | | |
67 | | // Compound assignment. |
68 | 252k | CharUnits& operator+= (const CharUnits &Other) { |
69 | 252k | Quantity += Other.Quantity; |
70 | 252k | return *this; |
71 | 252k | } |
72 | 1.57k | CharUnits& operator++ () { |
73 | 1.57k | ++Quantity; |
74 | 1.57k | return *this; |
75 | 1.57k | } |
76 | 328 | CharUnits operator++ (int) { |
77 | 328 | return CharUnits(Quantity++); |
78 | 328 | } |
79 | 1.82k | CharUnits& operator-= (const CharUnits &Other) { |
80 | 1.82k | Quantity -= Other.Quantity; |
81 | 1.82k | return *this; |
82 | 1.82k | } |
83 | 0 | CharUnits& operator-- () { |
84 | 0 | --Quantity; |
85 | 0 | return *this; |
86 | 0 | } |
87 | 0 | CharUnits operator-- (int) { |
88 | 0 | return CharUnits(Quantity--); |
89 | 0 | } |
90 | | |
91 | | // Comparison operators. |
92 | 6.52M | bool operator== (const CharUnits &Other) const { |
93 | 6.52M | return Quantity == Other.Quantity; |
94 | 6.52M | } |
95 | 497k | bool operator!= (const CharUnits &Other) const { |
96 | 497k | return Quantity != Other.Quantity; |
97 | 497k | } |
98 | | |
99 | | // Relational operators. |
100 | 8.50M | bool operator< (const CharUnits &Other) const { |
101 | 8.50M | return Quantity < Other.Quantity; |
102 | 8.50M | } |
103 | 1.78M | bool operator<= (const CharUnits &Other) const { |
104 | 1.78M | return Quantity <= Other.Quantity; |
105 | 1.78M | } |
106 | 5.12M | bool operator> (const CharUnits &Other) const { |
107 | 5.12M | return Quantity > Other.Quantity; |
108 | 5.12M | } |
109 | 1.04M | bool operator>= (const CharUnits &Other) const { |
110 | 1.04M | return Quantity >= Other.Quantity; |
111 | 1.04M | } |
112 | | |
113 | | // Other predicates. |
114 | | |
115 | | /// isZero - Test whether the quantity equals zero. |
116 | 26.2M | bool isZero() const { return Quantity == 0; } |
117 | | |
118 | | /// isOne - Test whether the quantity equals one. |
119 | 3.09k | bool isOne() const { return Quantity == 1; } |
120 | | |
121 | | /// isPositive - Test whether the quantity is greater than zero. |
122 | 5.77k | bool isPositive() const { return Quantity > 0; } |
123 | | |
124 | | /// isNegative - Test whether the quantity is less than zero. |
125 | 116k | bool isNegative() const { return Quantity < 0; } |
126 | | |
127 | | /// isPowerOfTwo - Test whether the quantity is a power of two. |
128 | | /// Zero is not a power of two. |
129 | 7.06M | bool isPowerOfTwo() const { |
130 | 7.06M | return (Quantity & -Quantity) == Quantity; |
131 | 7.06M | } |
132 | | |
133 | | /// Test whether this is a multiple of the other value. |
134 | | /// |
135 | | /// Among other things, this promises that |
136 | | /// self.alignTo(N) will just return self. |
137 | 25.2k | bool isMultipleOf(CharUnits N) const { |
138 | 25.2k | return (*this % N) == 0; |
139 | 25.2k | } |
140 | | |
141 | | // Arithmetic operators. |
142 | 157k | CharUnits operator* (QuantityType N) const { |
143 | 157k | return CharUnits(Quantity * N); |
144 | 157k | } |
145 | 4.34k | CharUnits &operator*= (QuantityType N) { |
146 | 4.34k | Quantity *= N; |
147 | 4.34k | return *this; |
148 | 4.34k | } |
149 | 511 | CharUnits operator/ (QuantityType N) const { |
150 | 511 | return CharUnits(Quantity / N); |
151 | 511 | } |
152 | 60 | CharUnits &operator/= (QuantityType N) { |
153 | 60 | Quantity /= N; |
154 | 60 | return *this; |
155 | 60 | } |
156 | 818 | QuantityType operator/ (const CharUnits &Other) const { |
157 | 818 | return Quantity / Other.Quantity; |
158 | 818 | } |
159 | 138 | CharUnits operator% (QuantityType N) const { |
160 | 138 | return CharUnits(Quantity % N); |
161 | 138 | } |
162 | 617k | QuantityType operator% (const CharUnits &Other) const { |
163 | 617k | return Quantity % Other.Quantity; |
164 | 617k | } |
165 | 4.85M | CharUnits operator+ (const CharUnits &Other) const { |
166 | 4.85M | return CharUnits(Quantity + Other.Quantity); |
167 | 4.85M | } |
168 | 49.1k | CharUnits operator- (const CharUnits &Other) const { |
169 | 49.1k | return CharUnits(Quantity - Other.Quantity); |
170 | 49.1k | } |
171 | 9.15k | CharUnits operator- () const { |
172 | 9.15k | return CharUnits(-Quantity); |
173 | 9.15k | } |
174 | | |
175 | | |
176 | | // Conversions. |
177 | | |
178 | | /// getQuantity - Get the raw integer representation of this quantity. |
179 | 35.8M | QuantityType getQuantity() const { return Quantity; } |
180 | | |
181 | | /// getAsAlign - Returns Quantity as a valid llvm::Align, |
182 | | /// Beware llvm::Align assumes power of two 8-bit bytes. |
183 | 4.55M | llvm::Align getAsAlign() const { return llvm::Align(Quantity); } |
184 | | |
185 | | /// getAsMaybeAlign - Returns Quantity as a valid llvm::Align or |
186 | | /// llvm::None, Beware llvm::MaybeAlign assumes power of two 8-bit bytes. |
187 | 375 | llvm::MaybeAlign getAsMaybeAlign() const { |
188 | 375 | return llvm::MaybeAlign(Quantity); |
189 | 375 | } |
190 | | |
191 | | /// alignTo - Returns the next integer (mod 2**64) that is |
192 | | /// greater than or equal to this quantity and is a multiple of \p Align. |
193 | | /// Align must be non-zero. |
194 | 2.14M | CharUnits alignTo(const CharUnits &Align) const { |
195 | 2.14M | return CharUnits(llvm::alignTo(Quantity, Align.Quantity)); |
196 | 2.14M | } |
197 | | |
198 | | /// Given that this is a non-zero alignment value, what is the |
199 | | /// alignment at the given offset? |
200 | 474k | CharUnits alignmentAtOffset(CharUnits offset) const { |
201 | 474k | assert(Quantity != 0 && "offsetting from unknown alignment?"); |
202 | 0 | return CharUnits(llvm::MinAlign(Quantity, offset.Quantity)); |
203 | 474k | } |
204 | | |
205 | | /// Given that this is the alignment of the first element of an |
206 | | /// array, return the minimum alignment of any element in the array. |
207 | 59.2k | CharUnits alignmentOfArrayElement(CharUnits elementSize) const { |
208 | | // Since we don't track offsetted alignments, the alignment of |
209 | | // the second element (or any odd element) will be minimally |
210 | | // aligned. |
211 | 59.2k | return alignmentAtOffset(elementSize); |
212 | 59.2k | } |
213 | | |
214 | | |
215 | | }; // class CharUnit |
216 | | } // namespace clang |
217 | | |
218 | | inline clang::CharUnits operator* (clang::CharUnits::QuantityType Scale, |
219 | 149k | const clang::CharUnits &CU) { |
220 | 149k | return CU * Scale; |
221 | 149k | } |
222 | | |
223 | | namespace llvm { |
224 | | |
225 | | template<> struct DenseMapInfo<clang::CharUnits> { |
226 | 895k | static clang::CharUnits getEmptyKey() { |
227 | 895k | clang::CharUnits::QuantityType Quantity = |
228 | 895k | DenseMapInfo<clang::CharUnits::QuantityType>::getEmptyKey(); |
229 | | |
230 | 895k | return clang::CharUnits::fromQuantity(Quantity); |
231 | 895k | } |
232 | | |
233 | 767k | static clang::CharUnits getTombstoneKey() { |
234 | 767k | clang::CharUnits::QuantityType Quantity = |
235 | 767k | DenseMapInfo<clang::CharUnits::QuantityType>::getTombstoneKey(); |
236 | | |
237 | 767k | return clang::CharUnits::fromQuantity(Quantity); |
238 | 767k | } |
239 | | |
240 | 740k | static unsigned getHashValue(const clang::CharUnits &CU) { |
241 | 740k | clang::CharUnits::QuantityType Quantity = CU.getQuantity(); |
242 | 740k | return DenseMapInfo<clang::CharUnits::QuantityType>::getHashValue(Quantity); |
243 | 740k | } |
244 | | |
245 | | static bool isEqual(const clang::CharUnits &LHS, |
246 | 5.36M | const clang::CharUnits &RHS) { |
247 | 5.36M | return LHS == RHS; |
248 | 5.36M | } |
249 | | }; |
250 | | |
251 | | } // end namespace llvm |
252 | | |
253 | | #endif // LLVM_CLANG_AST_CHARUNITS_H |