clang 24.0.0git
CharUnits.h
Go to the documentation of this file.
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
21namespace clang {
22
23/// 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.
38class CharUnits {
39public:
40 typedef int64_t QuantityType;
41
42private:
43 QuantityType Quantity = 0;
44
45 explicit CharUnits(QuantityType C) : Quantity(C) {}
46
47public:
48 /// A default constructor.
49 CharUnits() = default;
50
51 /// Construct a CharUnits quantity of zero.
52 static CharUnits Zero() { return CharUnits(0); }
53
54 /// Construct a CharUnits quantity of one.
55 static CharUnits One() { return CharUnits(1); }
56
57 /// Construct a CharUnits quantity from a raw integer type.
58 static CharUnits fromQuantity(QuantityType Quantity) {
59 return CharUnits(Quantity);
60 }
61
62 /// Construct a CharUnits quantity from an llvm::Align
63 /// quantity.
64 static CharUnits fromQuantity(llvm::Align Quantity) {
65 return CharUnits(Quantity.value());
66 }
67
68 // Compound assignment.
69 CharUnits &operator+=(CharUnits Other) {
70 Quantity += Other.Quantity;
71 return *this;
72 }
73 CharUnits &operator++() {
74 ++Quantity;
75 return *this;
76 }
77 CharUnits operator++(int) { return CharUnits(Quantity++); }
78 CharUnits &operator-=(CharUnits Other) {
79 Quantity -= Other.Quantity;
80 return *this;
81 }
82 CharUnits &operator--() {
83 --Quantity;
84 return *this;
85 }
86 CharUnits operator--(int) { return CharUnits(Quantity--); }
87
88 // Comparison operators.
89 bool operator==(CharUnits Other) const { return Quantity == Other.Quantity; }
90 bool operator!=(CharUnits Other) const { return Quantity != Other.Quantity; }
91
92 // Relational operators.
93 bool operator<(CharUnits Other) const { return Quantity < Other.Quantity; }
94 bool operator<=(CharUnits Other) const { return Quantity <= Other.Quantity; }
95 bool operator>(CharUnits Other) const { return Quantity > Other.Quantity; }
96 bool operator>=(CharUnits Other) const { return Quantity >= Other.Quantity; }
97
98 // Other predicates.
99
100 /// Test whether the quantity equals zero.
101 bool isZero() const { return Quantity == 0; }
102
103 /// Test whether the quantity equals one.
104 bool isOne() const { return Quantity == 1; }
105
106 /// Test whether the quantity is greater than zero.
107 bool isPositive() const { return Quantity > 0; }
108
109 /// Test whether the quantity is less than zero.
110 bool isNegative() const { return Quantity < 0; }
111
112 /// Test whether the quantity is a power of two.
113 /// Zero is not a power of two.
114 bool isPowerOfTwo() const {
115 return Quantity > 0 && llvm::isPowerOf2_64(Quantity);
116 }
117
118 /// Test whether this is a multiple of the other value.
119 ///
120 /// Among other things, this promises that
121 /// self.alignTo(N) will just return self.
122 bool isMultipleOf(CharUnits N) const {
123 return (*this % N) == CharUnits::Zero();
124 }
125
126 // Arithmetic operators.
127 CharUnits operator*(QuantityType N) const { return CharUnits(Quantity * N); }
128 CharUnits &operator*=(QuantityType N) {
129 Quantity *= N;
130 return *this;
131 }
132 CharUnits operator/(QuantityType N) const { return CharUnits(Quantity / N); }
133 CharUnits &operator/=(QuantityType N) {
134 Quantity /= N;
135 return *this;
136 }
137 QuantityType operator/(CharUnits Other) const {
138 return Quantity / Other.Quantity;
139 }
140 CharUnits operator%(QuantityType N) const { return CharUnits(Quantity % N); }
141 CharUnits operator%(CharUnits Other) const {
142 return CharUnits(Quantity % Other.Quantity);
143 }
144 CharUnits operator+(CharUnits Other) const {
145 return CharUnits(Quantity + Other.Quantity);
146 }
147 CharUnits operator-(CharUnits Other) const {
148 return CharUnits(Quantity - Other.Quantity);
149 }
150 CharUnits operator-() const { return CharUnits(-Quantity); }
151
152 // Conversions.
153
154 /// Get the raw integer representation of this quantity.
155 QuantityType getQuantity() const { return Quantity; }
156
157 /// Returns Quantity as a valid llvm::Align,
158 /// Beware llvm::Align assumes power of two 8-bit bytes.
159 llvm::Align getAsAlign() const { return llvm::Align(Quantity); }
160
161 /// Returns Quantity as a valid llvm::Align or
162 /// std::nullopt, Beware llvm::MaybeAlign assumes power of two 8-bit
163 /// bytes.
164 llvm::MaybeAlign getAsMaybeAlign() const {
165 return llvm::MaybeAlign(Quantity);
166 }
167
168 /// Returns the next integer (mod 2**64) that is
169 /// greater than or equal to this quantity and is a multiple of \p Align.
170 /// Align must be non-zero.
171 CharUnits alignTo(CharUnits Align) const {
172 return CharUnits(llvm::alignTo(Quantity, Align.Quantity));
173 }
174
175 /// Given that this is a non-zero alignment value, what is the
176 /// alignment at the given offset?
177 CharUnits alignmentAtOffset(CharUnits offset) const {
178 assert(Quantity != 0 && "offsetting from unknown alignment?");
179 return CharUnits(llvm::MinAlign(Quantity, offset.Quantity));
180 }
181
182 /// Given that this is the alignment of the first element of an
183 /// array, return the minimum alignment of any element in the array.
184 CharUnits alignmentOfArrayElement(CharUnits elementSize) const {
185 // Since we don't track offsetted alignments, the alignment of
186 // the second element (or any odd element) will be minimally
187 // aligned.
188 return alignmentAtOffset(elementSize);
189 }
190
191}; // class CharUnit
192} // namespace clang
193
195 clang::CharUnits CU) {
196 return CU * Scale;
197}
198
199namespace llvm {
200
201template<> struct DenseMapInfo<clang::CharUnits> {
202 static unsigned getHashValue(clang::CharUnits CU) {
204 return DenseMapInfo<clang::CharUnits::QuantityType>::getHashValue(Quantity);
205 }
206
208 return LHS == RHS;
209 }
210};
211
212} // end namespace llvm
213
214#endif // LLVM_CLANG_AST_CHARUNITS_H
clang::CharUnits operator*(clang::CharUnits::QuantityType Scale, clang::CharUnits CU)
Definition CharUnits.h:194
This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
CharUnits operator-() const
Definition CharUnits.h:150
bool isPowerOfTwo() const
Test whether the quantity is a power of two.
Definition CharUnits.h:114
CharUnits alignmentAtOffset(CharUnits offset) const
Given that this is a non-zero alignment value, what is the alignment at the given offset?
Definition CharUnits.h:177
bool isNegative() const
Test whether the quantity is less than zero.
Definition CharUnits.h:110
CharUnits & operator++()
Definition CharUnits.h:73
CharUnits & operator+=(CharUnits Other)
Definition CharUnits.h:69
CharUnits & operator*=(QuantityType N)
Definition CharUnits.h:128
bool operator!=(CharUnits Other) const
Definition CharUnits.h:90
CharUnits & operator-=(CharUnits Other)
Definition CharUnits.h:78
CharUnits operator%(CharUnits Other) const
Definition CharUnits.h:141
int64_t QuantityType
Definition CharUnits.h:40
CharUnits & operator--()
Definition CharUnits.h:82
bool isPositive() const
Test whether the quantity is greater than zero.
Definition CharUnits.h:107
bool isZero() const
Test whether the quantity equals zero.
Definition CharUnits.h:101
CharUnits operator++(int)
Definition CharUnits.h:77
bool operator==(CharUnits Other) const
Definition CharUnits.h:89
CharUnits()=default
A default constructor.
CharUnits alignTo(CharUnits Align) const
Returns the next integer (mod 2**64) that is greater than or equal to this quantity and is a multiple...
Definition CharUnits.h:171
llvm::MaybeAlign getAsMaybeAlign() const
Returns Quantity as a valid llvm::Align or std::nullopt, Beware llvm::MaybeAlign assumes power of two...
Definition CharUnits.h:164
llvm::Align getAsAlign() const
Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit bytes.
Definition CharUnits.h:159
QuantityType operator/(CharUnits Other) const
Definition CharUnits.h:137
QuantityType getQuantity() const
Get the raw integer representation of this quantity.
Definition CharUnits.h:155
static CharUnits One()
Construct a CharUnits quantity of one.
Definition CharUnits.h:55
CharUnits operator%(QuantityType N) const
Definition CharUnits.h:140
CharUnits operator+(CharUnits Other) const
Definition CharUnits.h:144
static CharUnits fromQuantity(llvm::Align Quantity)
Construct a CharUnits quantity from an llvm::Align quantity.
Definition CharUnits.h:64
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition CharUnits.h:184
bool isOne() const
Test whether the quantity equals one.
Definition CharUnits.h:104
CharUnits operator-(CharUnits Other) const
Definition CharUnits.h:147
CharUnits operator*(QuantityType N) const
Definition CharUnits.h:127
bool operator<=(CharUnits Other) const
Definition CharUnits.h:94
bool operator>=(CharUnits Other) const
Definition CharUnits.h:96
CharUnits operator/(QuantityType N) const
Definition CharUnits.h:132
bool isMultipleOf(CharUnits N) const
Test whether this is a multiple of the other value.
Definition CharUnits.h:122
static CharUnits fromQuantity(QuantityType Quantity)
Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:58
bool operator<(CharUnits Other) const
Definition CharUnits.h:93
static CharUnits Zero()
Construct a CharUnits quantity of zero.
Definition CharUnits.h:52
bool operator>(CharUnits Other) const
Definition CharUnits.h:95
CharUnits operator--(int)
Definition CharUnits.h:86
CharUnits & operator/=(QuantityType N)
Definition CharUnits.h:133
Top level wrappers for InstallAPI frontend operations.
@ Other
Other implicit parameter.
Definition Decl.h:1775
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
static unsigned getHashValue(clang::CharUnits CU)
Definition CharUnits.h:202
static bool isEqual(clang::CharUnits LHS, clang::CharUnits RHS)
Definition CharUnits.h:207