clang 20.0.0git
Integral.h
Go to the documentation of this file.
1//===--- Integral.h - Wrapper for numeric types for the VM ------*- 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// Defines the VM types and helpers operating on types.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_INTEGRAL_H
14#define LLVM_CLANG_AST_INTERP_INTEGRAL_H
15
16#include "clang/AST/APValue.h"
18#include "llvm/ADT/APSInt.h"
19#include "llvm/Support/MathExtras.h"
20#include "llvm/Support/raw_ostream.h"
21#include <cstddef>
22#include <cstdint>
23
24#include "Primitives.h"
25
26namespace clang {
27namespace interp {
28
29using APInt = llvm::APInt;
30using APSInt = llvm::APSInt;
31
32template <bool Signed> class IntegralAP;
33
34// Helper structure to select the representation.
35template <unsigned Bits, bool Signed> struct Repr;
36template <> struct Repr<8, false> {
37 using Type = uint8_t;
38};
39template <> struct Repr<16, false> {
40 using Type = uint16_t;
41};
42template <> struct Repr<32, false> {
43 using Type = uint32_t;
44};
45template <> struct Repr<64, false> {
46 using Type = uint64_t;
47};
48template <> struct Repr<8, true> {
49 using Type = int8_t;
50};
51template <> struct Repr<16, true> {
52 using Type = int16_t;
53};
54template <> struct Repr<32, true> {
55 using Type = int32_t;
56};
57template <> struct Repr<64, true> {
58 using Type = int64_t;
59};
60
61/// Wrapper around numeric types.
62///
63/// These wrappers are required to shared an interface between APSint and
64/// builtin primitive numeral types, while optimising for storage and
65/// allowing methods operating on primitive type to compile to fast code.
66template <unsigned Bits, bool Signed> class Integral final {
67private:
68 template <unsigned OtherBits, bool OtherSigned> friend class Integral;
69
70 // The primitive representing the integral.
71 using ReprT = typename Repr<Bits, Signed>::Type;
72 ReprT V;
73
74 /// Primitive representing limits.
75 static const auto Min = std::numeric_limits<ReprT>::min();
76 static const auto Max = std::numeric_limits<ReprT>::max();
77
78 /// Construct an integral from anything that is convertible to storage.
79 template <typename T> explicit Integral(T V) : V(V) {}
80
81public:
83
84 /// Zero-initializes an integral.
85 Integral() : V(0) {}
86
87 /// Constructs an integral from another integral.
88 template <unsigned SrcBits, bool SrcSign>
90
91 /// Construct an integral from a value based on signedness.
92 explicit Integral(const APSInt &V)
93 : V(V.isSigned() ? V.getSExtValue() : V.getZExtValue()) {}
94
95 bool operator<(Integral RHS) const { return V < RHS.V; }
96 bool operator>(Integral RHS) const { return V > RHS.V; }
97 bool operator<=(Integral RHS) const { return V <= RHS.V; }
98 bool operator>=(Integral RHS) const { return V >= RHS.V; }
99 bool operator==(Integral RHS) const { return V == RHS.V; }
100 bool operator!=(Integral RHS) const { return V != RHS.V; }
101
102 bool operator>(unsigned RHS) const {
103 return V >= 0 && static_cast<unsigned>(V) > RHS;
104 }
105
106 Integral operator-() const { return Integral(-V); }
108 return Integral(V - Other.V);
109 }
110 Integral operator~() const { return Integral(~V); }
111
112 template <unsigned DstBits, bool DstSign>
113 explicit operator Integral<DstBits, DstSign>() const {
115 }
116
117 template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>>
118 explicit operator Ty() const {
119 return V;
120 }
121
122 APSInt toAPSInt() const {
123 return APSInt(APInt(Bits, static_cast<uint64_t>(V), Signed), !Signed);
124 }
125 APSInt toAPSInt(unsigned NumBits) const {
126 if constexpr (Signed)
127 return APSInt(toAPSInt().sextOrTrunc(NumBits), !Signed);
128 else
129 return APSInt(toAPSInt().zextOrTrunc(NumBits), !Signed);
130 }
131 APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); }
132
134 return Integral<Bits, false>(*this);
135 }
136
137 constexpr static unsigned bitWidth() { return Bits; }
138
139 bool isZero() const { return !V; }
140
141 bool isMin() const { return *this == min(bitWidth()); }
142
143 bool isMinusOne() const { return Signed && V == ReprT(-1); }
144
145 constexpr static bool isSigned() { return Signed; }
146
147 bool isNegative() const { return V < ReprT(0); }
148 bool isPositive() const { return !isNegative(); }
149
151 return Compare(V, RHS.V);
152 }
153
154 std::string toDiagnosticString(const ASTContext &Ctx) const {
155 std::string NameStr;
156 llvm::raw_string_ostream OS(NameStr);
157 OS << V;
158 return NameStr;
159 }
160
161 unsigned countLeadingZeros() const {
162 if constexpr (!Signed)
163 return llvm::countl_zero<ReprT>(V);
164 llvm_unreachable("Don't call countLeadingZeros() on signed types.");
165 }
166
167 Integral truncate(unsigned TruncBits) const {
168 if (TruncBits >= Bits)
169 return *this;
170 const ReprT BitMask = (ReprT(1) << ReprT(TruncBits)) - 1;
171 const ReprT SignBit = ReprT(1) << (TruncBits - 1);
172 const ReprT ExtMask = ~BitMask;
173 return Integral((V & BitMask) | (Signed && (V & SignBit) ? ExtMask : 0));
174 }
175
176 void print(llvm::raw_ostream &OS) const { OS << V; }
177
178 static Integral min(unsigned NumBits) { return Integral(Min); }
179 static Integral max(unsigned NumBits) { return Integral(Max); }
180
181 template <typename ValT> static Integral from(ValT Value) {
182 if constexpr (std::is_integral<ValT>::value)
183 return Integral(Value);
184 else
185 return Integral::from(static_cast<Integral::ReprT>(Value));
186 }
187
188 template <unsigned SrcBits, bool SrcSign>
189 static std::enable_if_t<SrcBits != 0, Integral>
191 return Integral(Value.V);
192 }
193
194 static Integral zero() { return from(0); }
195
196 template <typename T> static Integral from(T Value, unsigned NumBits) {
197 return Integral(Value);
198 }
199
200 static bool inRange(int64_t Value, unsigned NumBits) {
201 return CheckRange<ReprT, Min, Max>(Value);
202 }
203
204 static bool increment(Integral A, Integral *R) {
205 return add(A, Integral(ReprT(1)), A.bitWidth(), R);
206 }
207
208 static bool decrement(Integral A, Integral *R) {
209 return sub(A, Integral(ReprT(1)), A.bitWidth(), R);
210 }
211
212 static bool add(Integral A, Integral B, unsigned OpBits, Integral *R) {
213 return CheckAddUB(A.V, B.V, R->V);
214 }
215
216 static bool sub(Integral A, Integral B, unsigned OpBits, Integral *R) {
217 return CheckSubUB(A.V, B.V, R->V);
218 }
219
220 static bool mul(Integral A, Integral B, unsigned OpBits, Integral *R) {
221 return CheckMulUB(A.V, B.V, R->V);
222 }
223
224 static bool rem(Integral A, Integral B, unsigned OpBits, Integral *R) {
225 *R = Integral(A.V % B.V);
226 return false;
227 }
228
229 static bool div(Integral A, Integral B, unsigned OpBits, Integral *R) {
230 *R = Integral(A.V / B.V);
231 return false;
232 }
233
234 static bool bitAnd(Integral A, Integral B, unsigned OpBits, Integral *R) {
235 *R = Integral(A.V & B.V);
236 return false;
237 }
238
239 static bool bitOr(Integral A, Integral B, unsigned OpBits, Integral *R) {
240 *R = Integral(A.V | B.V);
241 return false;
242 }
243
244 static bool bitXor(Integral A, Integral B, unsigned OpBits, Integral *R) {
245 *R = Integral(A.V ^ B.V);
246 return false;
247 }
248
249 static bool neg(Integral A, Integral *R) {
250 if (Signed && A.isMin())
251 return true;
252
253 *R = -A;
254 return false;
255 }
256
257 static bool comp(Integral A, Integral *R) {
258 *R = Integral(~A.V);
259 return false;
260 }
261
262 template <unsigned RHSBits, bool RHSSign>
263 static void shiftLeft(const Integral A, const Integral<RHSBits, RHSSign> B,
264 unsigned OpBits, Integral *R) {
265 *R = Integral::from(A.V << B.V, OpBits);
266 }
267
268 template <unsigned RHSBits, bool RHSSign>
269 static void shiftRight(const Integral A, const Integral<RHSBits, RHSSign> B,
270 unsigned OpBits, Integral *R) {
271 *R = Integral::from(A.V >> B.V, OpBits);
272 }
273
274private:
275 template <typename T> static bool CheckAddUB(T A, T B, T &R) {
276 if constexpr (std::is_signed_v<T>) {
277 return llvm::AddOverflow<T>(A, B, R);
278 } else {
279 R = A + B;
280 return false;
281 }
282 }
283
284 template <typename T> static bool CheckSubUB(T A, T B, T &R) {
285 if constexpr (std::is_signed_v<T>) {
286 return llvm::SubOverflow<T>(A, B, R);
287 } else {
288 R = A - B;
289 return false;
290 }
291 }
292
293 template <typename T> static bool CheckMulUB(T A, T B, T &R) {
294 if constexpr (std::is_signed_v<T>) {
295 return llvm::MulOverflow<T>(A, B, R);
296 } else {
297 R = A * B;
298 return false;
299 }
300 }
301 template <typename T, T Min, T Max> static bool CheckRange(int64_t V) {
302 if constexpr (std::is_signed_v<T>) {
303 return Min <= V && V <= Max;
304 } else {
305 return V >= 0 && static_cast<uint64_t>(V) <= Max;
306 }
307 }
308};
309
310template <unsigned Bits, bool Signed>
311llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, Integral<Bits, Signed> I) {
312 I.print(OS);
313 return OS;
314}
315
316} // namespace interp
317} // namespace clang
318
319#endif
#define V(N, I)
Definition: ASTContext.h:3341
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
Wrapper around numeric types.
Definition: Integral.h:66
Integral< Bits, false > toUnsigned() const
Definition: Integral.h:133
bool isMinusOne() const
Definition: Integral.h:143
bool isNegative() const
Definition: Integral.h:147
static Integral max(unsigned NumBits)
Definition: Integral.h:179
Integral()
Zero-initializes an integral.
Definition: Integral.h:85
std::string toDiagnosticString(const ASTContext &Ctx) const
Definition: Integral.h:154
static bool mul(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:220
bool operator>=(Integral RHS) const
Definition: Integral.h:98
friend class Integral
Definition: Integral.h:68
void print(llvm::raw_ostream &OS) const
Definition: Integral.h:176
static bool sub(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:216
static constexpr unsigned bitWidth()
Definition: Integral.h:137
APSInt toAPSInt() const
Definition: Integral.h:122
static void shiftRight(const Integral A, const Integral< RHSBits, RHSSign > B, unsigned OpBits, Integral *R)
Definition: Integral.h:269
bool operator>(Integral RHS) const
Definition: Integral.h:96
bool operator>(unsigned RHS) const
Definition: Integral.h:102
Integral operator-(const Integral &Other) const
Definition: Integral.h:107
bool isZero() const
Definition: Integral.h:139
unsigned countLeadingZeros() const
Definition: Integral.h:161
Integral operator~() const
Definition: Integral.h:110
Integral truncate(unsigned TruncBits) const
Definition: Integral.h:167
bool operator<(Integral RHS) const
Definition: Integral.h:95
ComparisonCategoryResult compare(const Integral &RHS) const
Definition: Integral.h:150
static Integral from(ValT Value)
Definition: Integral.h:181
bool isMin() const
Definition: Integral.h:141
static bool neg(Integral A, Integral *R)
Definition: Integral.h:249
bool operator!=(Integral RHS) const
Definition: Integral.h:100
APValue toAPValue(const ASTContext &) const
Definition: Integral.h:131
static bool decrement(Integral A, Integral *R)
Definition: Integral.h:208
static bool comp(Integral A, Integral *R)
Definition: Integral.h:257
Integral(Integral< SrcBits, SrcSign > V)
Constructs an integral from another integral.
Definition: Integral.h:89
static Integral from(T Value, unsigned NumBits)
Definition: Integral.h:196
bool operator<=(Integral RHS) const
Definition: Integral.h:97
static bool div(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:229
static bool inRange(int64_t Value, unsigned NumBits)
Definition: Integral.h:200
APSInt toAPSInt(unsigned NumBits) const
Definition: Integral.h:125
static bool bitXor(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:244
static bool rem(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:224
bool operator==(Integral RHS) const
Definition: Integral.h:99
Integral(const APSInt &V)
Construct an integral from a value based on signedness.
Definition: Integral.h:92
static Integral min(unsigned NumBits)
Definition: Integral.h:178
static Integral zero()
Definition: Integral.h:194
static constexpr bool isSigned()
Definition: Integral.h:145
static void shiftLeft(const Integral A, const Integral< RHSBits, RHSSign > B, unsigned OpBits, Integral *R)
Definition: Integral.h:263
static bool bitOr(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:239
Integral operator-() const
Definition: Integral.h:106
bool isPositive() const
Definition: Integral.h:148
static std::enable_if_t< SrcBits !=0, Integral > from(Integral< SrcBits, SrcSign > Value)
Definition: Integral.h:190
static bool add(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:212
static bool increment(Integral A, Integral *R)
Definition: Integral.h:204
static bool bitAnd(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:234
llvm::APInt APInt
Definition: Integral.h:29
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:151
ComparisonCategoryResult Compare(const T &X, const T &Y)
Helper to compare two comparable types.
Definition: Primitives.h:25
llvm::APSInt APSInt
Definition: Floating.h:24
The JSON file list parser is used to communicate input to InstallAPI.
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
const FunctionProtoType * T
@ Other
Other implicit parameter.
unsigned long uint64_t
#define true
Definition: stdbool.h:25
#define false
Definition: stdbool.h:26