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