Coverage Report

Created: 2025-12-12 07:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hermes/external/llvh/include/llvh/ADT/APSInt.h
Line
Count
Source
1
//===-- llvm/ADT/APSInt.h - Arbitrary Precision Signed Int -----*- C++ -*--===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file implements the APSInt class, which is a simple class that
11
// represents an arbitrary sized integer that knows its signedness.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_ADT_APSINT_H
16
#define LLVM_ADT_APSINT_H
17
18
#include "llvh/ADT/APInt.h"
19
20
namespace llvh {
21
22
class LLVM_NODISCARD APSInt : public APInt {
23
  bool IsUnsigned;
24
25
public:
26
  /// Default constructor that creates an uninitialized APInt.
27
0
  explicit APSInt() : IsUnsigned(false) {}
28
29
  /// APSInt ctor - Create an APSInt with the specified width, default to
30
  /// unsigned.
31
  explicit APSInt(uint32_t BitWidth, bool isUnsigned = true)
32
0
   : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
33
34
  explicit APSInt(APInt I, bool isUnsigned = true)
35
0
   : APInt(std::move(I)), IsUnsigned(isUnsigned) {}
36
37
  /// Construct an APSInt from a string representation.
38
  ///
39
  /// This constructor interprets the string \p Str using the radix of 10.
40
  /// The interpretation stops at the end of the string. The bit width of the
41
  /// constructed APSInt is determined automatically.
42
  ///
43
  /// \param Str the string to be interpreted.
44
  explicit APSInt(StringRef Str);
45
46
0
  APSInt &operator=(APInt RHS) {
47
    // Retain our current sign.
48
0
    APInt::operator=(std::move(RHS));
49
0
    return *this;
50
0
  }
51
52
0
  APSInt &operator=(uint64_t RHS) {
53
0
    // Retain our current sign.
54
0
    APInt::operator=(RHS);
55
0
    return *this;
56
0
  }
57
58
  // Query sign information.
59
0
  bool isSigned() const { return !IsUnsigned; }
60
0
  bool isUnsigned() const { return IsUnsigned; }
61
0
  void setIsUnsigned(bool Val) { IsUnsigned = Val; }
62
0
  void setIsSigned(bool Val) { IsUnsigned = !Val; }
63
64
  /// toString - Append this APSInt to the specified SmallString.
65
0
  void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
66
0
    APInt::toString(Str, Radix, isSigned());
67
0
  }
68
  /// toString - Converts an APInt to a std::string.  This is an inefficient
69
  /// method; you should prefer passing in a SmallString instead.
70
0
  std::string toString(unsigned Radix) const {
71
0
    return APInt::toString(Radix, isSigned());
72
0
  }
73
  using APInt::toString;
74
75
  /// Get the correctly-extended \c int64_t value.
76
0
  int64_t getExtValue() const {
77
0
    assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
78
0
    return isSigned() ? getSExtValue() : getZExtValue();
79
0
  }
80
81
0
  APSInt trunc(uint32_t width) const {
82
0
    return APSInt(APInt::trunc(width), IsUnsigned);
83
0
  }
84
85
0
  APSInt extend(uint32_t width) const {
86
0
    if (IsUnsigned)
87
0
      return APSInt(zext(width), IsUnsigned);
88
0
    else
89
0
      return APSInt(sext(width), IsUnsigned);
90
0
  }
91
92
0
  APSInt extOrTrunc(uint32_t width) const {
93
0
    if (IsUnsigned)
94
0
      return APSInt(zextOrTrunc(width), IsUnsigned);
95
0
    else
96
0
      return APSInt(sextOrTrunc(width), IsUnsigned);
97
0
  }
98
99
0
  const APSInt &operator%=(const APSInt &RHS) {
100
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
101
0
    if (IsUnsigned)
102
0
      *this = urem(RHS);
103
0
    else
104
0
      *this = srem(RHS);
105
0
    return *this;
106
0
  }
107
0
  const APSInt &operator/=(const APSInt &RHS) {
108
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
109
0
    if (IsUnsigned)
110
0
      *this = udiv(RHS);
111
0
    else
112
0
      *this = sdiv(RHS);
113
0
    return *this;
114
0
  }
115
0
  APSInt operator%(const APSInt &RHS) const {
116
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
117
0
    return IsUnsigned ? APSInt(urem(RHS), true) : APSInt(srem(RHS), false);
118
0
  }
119
0
  APSInt operator/(const APSInt &RHS) const {
120
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
121
0
    return IsUnsigned ? APSInt(udiv(RHS), true) : APSInt(sdiv(RHS), false);
122
0
  }
123
124
0
  APSInt operator>>(unsigned Amt) const {
125
0
    return IsUnsigned ? APSInt(lshr(Amt), true) : APSInt(ashr(Amt), false);
126
0
  }
127
0
  APSInt& operator>>=(unsigned Amt) {
128
0
    if (IsUnsigned)
129
0
      lshrInPlace(Amt);
130
0
    else
131
0
      ashrInPlace(Amt);
132
0
    return *this;
133
0
  }
134
135
0
  inline bool operator<(const APSInt& RHS) const {
136
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
137
0
    return IsUnsigned ? ult(RHS) : slt(RHS);
138
0
  }
139
0
  inline bool operator>(const APSInt& RHS) const {
140
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
141
0
    return IsUnsigned ? ugt(RHS) : sgt(RHS);
142
0
  }
143
0
  inline bool operator<=(const APSInt& RHS) const {
144
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
145
0
    return IsUnsigned ? ule(RHS) : sle(RHS);
146
0
  }
147
0
  inline bool operator>=(const APSInt& RHS) const {
148
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
149
0
    return IsUnsigned ? uge(RHS) : sge(RHS);
150
0
  }
151
0
  inline bool operator==(const APSInt& RHS) const {
152
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
153
0
    return eq(RHS);
154
0
  }
155
0
  inline bool operator!=(const APSInt& RHS) const {
156
0
    return !((*this) == RHS);
157
0
  }
158
159
0
  bool operator==(int64_t RHS) const {
160
0
    return compareValues(*this, get(RHS)) == 0;
161
0
  }
162
0
  bool operator!=(int64_t RHS) const {
163
0
    return compareValues(*this, get(RHS)) != 0;
164
0
  }
165
0
  bool operator<=(int64_t RHS) const {
166
0
    return compareValues(*this, get(RHS)) <= 0;
167
0
  }
168
0
  bool operator>=(int64_t RHS) const {
169
0
    return compareValues(*this, get(RHS)) >= 0;
170
0
  }
171
0
  bool operator<(int64_t RHS) const {
172
0
    return compareValues(*this, get(RHS)) < 0;
173
0
  }
174
0
  bool operator>(int64_t RHS) const {
175
0
    return compareValues(*this, get(RHS)) > 0;
176
0
  }
177
178
  // The remaining operators just wrap the logic of APInt, but retain the
179
  // signedness information.
180
181
0
  APSInt operator<<(unsigned Bits) const {
182
0
    return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
183
0
  }
184
0
  APSInt& operator<<=(unsigned Amt) {
185
0
    static_cast<APInt&>(*this) <<= Amt;
186
0
    return *this;
187
0
  }
188
189
0
  APSInt& operator++() {
190
0
    ++(static_cast<APInt&>(*this));
191
0
    return *this;
192
0
  }
193
0
  APSInt& operator--() {
194
0
    --(static_cast<APInt&>(*this));
195
0
    return *this;
196
0
  }
197
0
  APSInt operator++(int) {
198
0
    return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
199
0
  }
200
0
  APSInt operator--(int) {
201
0
    return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
202
0
  }
203
0
  APSInt operator-() const {
204
0
    return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
205
0
  }
206
0
  APSInt& operator+=(const APSInt& RHS) {
207
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
208
0
    static_cast<APInt&>(*this) += RHS;
209
0
    return *this;
210
0
  }
211
0
  APSInt& operator-=(const APSInt& RHS) {
212
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
213
0
    static_cast<APInt&>(*this) -= RHS;
214
0
    return *this;
215
0
  }
216
0
  APSInt& operator*=(const APSInt& RHS) {
217
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
218
0
    static_cast<APInt&>(*this) *= RHS;
219
0
    return *this;
220
0
  }
221
0
  APSInt& operator&=(const APSInt& RHS) {
222
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
223
0
    static_cast<APInt&>(*this) &= RHS;
224
0
    return *this;
225
0
  }
226
0
  APSInt& operator|=(const APSInt& RHS) {
227
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
228
0
    static_cast<APInt&>(*this) |= RHS;
229
0
    return *this;
230
0
  }
231
0
  APSInt& operator^=(const APSInt& RHS) {
232
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
233
0
    static_cast<APInt&>(*this) ^= RHS;
234
0
    return *this;
235
0
  }
236
237
0
  APSInt operator&(const APSInt& RHS) const {
238
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
239
0
    return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
240
0
  }
241
242
0
  APSInt operator|(const APSInt& RHS) const {
243
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
244
0
    return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
245
0
  }
246
247
0
  APSInt operator^(const APSInt &RHS) const {
248
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
249
0
    return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
250
0
  }
251
252
0
  APSInt operator*(const APSInt& RHS) const {
253
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
254
0
    return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
255
0
  }
256
0
  APSInt operator+(const APSInt& RHS) const {
257
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
258
0
    return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
259
0
  }
260
0
  APSInt operator-(const APSInt& RHS) const {
261
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
262
0
    return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
263
0
  }
264
0
  APSInt operator~() const {
265
0
    return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
266
0
  }
267
268
  /// getMaxValue - Return the APSInt representing the maximum integer value
269
  ///  with the given bit width and signedness.
270
0
  static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
271
0
    return APSInt(Unsigned ? APInt::getMaxValue(numBits)
272
0
                           : APInt::getSignedMaxValue(numBits), Unsigned);
273
0
  }
274
275
  /// getMinValue - Return the APSInt representing the minimum integer value
276
  ///  with the given bit width and signedness.
277
0
  static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
278
0
    return APSInt(Unsigned ? APInt::getMinValue(numBits)
279
0
                           : APInt::getSignedMinValue(numBits), Unsigned);
280
0
  }
281
282
  /// Determine if two APSInts have the same value, zero- or
283
  /// sign-extending as needed.
284
0
  static bool isSameValue(const APSInt &I1, const APSInt &I2) {
285
0
    return !compareValues(I1, I2);
286
0
  }
287
288
  /// Compare underlying values of two numbers.
289
0
  static int compareValues(const APSInt &I1, const APSInt &I2) {
290
0
    if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
291
0
      return I1.IsUnsigned ? I1.compare(I2) : I1.compareSigned(I2);
292
0
293
0
    // Check for a bit-width mismatch.
294
0
    if (I1.getBitWidth() > I2.getBitWidth())
295
0
      return compareValues(I1, I2.extend(I1.getBitWidth()));
296
0
    if (I2.getBitWidth() > I1.getBitWidth())
297
0
      return compareValues(I1.extend(I2.getBitWidth()), I2);
298
0
299
0
    // We have a signedness mismatch. Check for negative values and do an
300
0
    // unsigned compare if both are positive.
301
0
    if (I1.isSigned()) {
302
0
      assert(!I2.isSigned() && "Expected signed mismatch");
303
0
      if (I1.isNegative())
304
0
        return -1;
305
0
    } else {
306
0
      assert(I2.isSigned() && "Expected signed mismatch");
307
0
      if (I2.isNegative())
308
0
        return 1;
309
0
    }
310
0
311
0
    return I1.compare(I2);
312
0
  }
313
314
0
  static APSInt get(int64_t X) { return APSInt(APInt(64, X), false); }
315
0
  static APSInt getUnsigned(uint64_t X) { return APSInt(APInt(64, X), true); }
316
317
  /// Profile - Used to insert APSInt objects, or objects that contain APSInt
318
  ///  objects, into FoldingSets.
319
  void Profile(FoldingSetNodeID& ID) const;
320
};
321
322
0
inline bool operator==(int64_t V1, const APSInt &V2) { return V2 == V1; }
323
0
inline bool operator!=(int64_t V1, const APSInt &V2) { return V2 != V1; }
324
0
inline bool operator<=(int64_t V1, const APSInt &V2) { return V2 >= V1; }
325
0
inline bool operator>=(int64_t V1, const APSInt &V2) { return V2 <= V1; }
326
0
inline bool operator<(int64_t V1, const APSInt &V2) { return V2 > V1; }
327
0
inline bool operator>(int64_t V1, const APSInt &V2) { return V2 < V1; }
328
329
0
inline raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
330
0
  I.print(OS, I.isSigned());
331
0
  return OS;
332
0
}
333
334
} // end namespace llvh
335
336
#endif