clang  10.0.0svn
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 namespace clang {
25 namespace interp {
26 
29 
30 /// Helper to compare two comparable types.
31 template <typename T>
32 ComparisonCategoryResult Compare(const T &X, const T &Y) {
33  if (X < Y)
35  if (X > Y)
38 }
39 
40 // Helper structure to select the representation.
41 template <unsigned Bits, bool Signed> struct Repr;
42 template <> struct Repr<8, false> { using Type = uint8_t; };
43 template <> struct Repr<16, false> { using Type = uint16_t; };
44 template <> struct Repr<32, false> { using Type = uint32_t; };
45 template <> struct Repr<64, false> { using Type = uint64_t; };
46 template <> struct Repr<8, true> { using Type = int8_t; };
47 template <> struct Repr<16, true> { using Type = int16_t; };
48 template <> struct Repr<32, true> { using Type = int32_t; };
49 template <> struct Repr<64, true> { using Type = int64_t; };
50 
51 /// Wrapper around numeric types.
52 ///
53 /// These wrappers are required to shared an interface between APSint and
54 /// builtin primitive numeral types, while optimising for storage and
55 /// allowing methods operating on primitive type to compile to fast code.
56 template <unsigned Bits, bool Signed> class Integral {
57 private:
58  template <unsigned OtherBits, bool OtherSigned> friend class Integral;
59 
60  // The primitive representing the integral.
61  using T = typename Repr<Bits, Signed>::Type;
62  T V;
63 
64  /// Primitive representing limits.
65  static const auto Min = std::numeric_limits<T>::min();
66  static const auto Max = std::numeric_limits<T>::max();
67 
68  /// Construct an integral from anything that is convertible to storage.
69  template <typename T> explicit Integral(T V) : V(V) {}
70 
71 public:
72  /// Zero-initializes an integral.
73  Integral() : V(0) {}
74 
75  /// Constructs an integral from another integral.
76  template <unsigned SrcBits, bool SrcSign>
77  explicit Integral(Integral<SrcBits, SrcSign> V) : V(V.V) {}
78 
79  /// Construct an integral from a value based on signedness.
80  explicit Integral(const APSInt &V)
81  : V(V.isSigned() ? V.getSExtValue() : V.getZExtValue()) {}
82 
83  bool operator<(Integral RHS) const { return V < RHS.V; }
84  bool operator>(Integral RHS) const { return V > RHS.V; }
85  bool operator<=(Integral RHS) const { return V <= RHS.V; }
86  bool operator>=(Integral RHS) const { return V >= RHS.V; }
87  bool operator==(Integral RHS) const { return V == RHS.V; }
88  bool operator!=(Integral RHS) const { return V != RHS.V; }
89 
90  bool operator>(unsigned RHS) const {
91  return V >= 0 && static_cast<unsigned>(V) > RHS;
92  }
93 
94  Integral operator-() const { return Integral(-V); }
95  Integral operator~() const { return Integral(~V); }
96 
97  template <unsigned DstBits, bool DstSign>
98  explicit operator Integral<DstBits, DstSign>() const {
100  }
101 
102  explicit operator unsigned() const { return V; }
103  explicit operator int64_t() const { return V; }
104  explicit operator uint64_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 (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 == T(-1); }
128 
129  constexpr static bool isSigned() { return Signed; }
130 
131  bool isNegative() const { return V < T(0); }
132  bool isPositive() const { return !isNegative(); }
133 
135  return Compare(V, RHS.V);
136  }
137 
138  unsigned countLeadingZeros() const { return llvm::countLeadingZeros<T>(V); }
139 
140  Integral truncate(unsigned TruncBits) const {
141  if (TruncBits >= Bits)
142  return *this;
143  const T BitMask = (T(1) << T(TruncBits)) - 1;
144  const T SignBit = T(1) << (TruncBits - 1);
145  const T ExtMask = ~BitMask;
146  return Integral((V & BitMask) | (Signed && (V & SignBit) ? ExtMask : 0));
147  }
148 
149  void print(llvm::raw_ostream &OS) const { OS << V; }
150 
151  static Integral min(unsigned NumBits) {
152  return Integral(Min);
153  }
154  static Integral max(unsigned NumBits) {
155  return Integral(Max);
156  }
157 
158  template <typename T>
159  static typename std::enable_if<std::is_integral<T>::value, Integral>::type
160  from(T Value) {
161  return Integral(Value);
162  }
163 
164  template <unsigned SrcBits, bool SrcSign>
167  return Integral(Value.V);
168  }
169 
170  template <bool SrcSign> static Integral from(Integral<0, SrcSign> Value) {
171  if (SrcSign)
172  return Integral(Value.V.getSExtValue());
173  else
174  return Integral(Value.V.getZExtValue());
175  }
176 
177  static Integral zero() { return from(0); }
178 
179  template <typename T> static Integral from(T Value, unsigned NumBits) {
180  return Integral(Value);
181  }
182 
183  static bool inRange(int64_t Value, unsigned NumBits) {
184  return CheckRange<T, Min, Max>(Value);
185  }
186 
187  static bool increment(Integral A, Integral *R) {
188  return add(A, Integral(T(1)), A.bitWidth(), R);
189  }
190 
191  static bool decrement(Integral A, Integral *R) {
192  return sub(A, Integral(T(1)), A.bitWidth(), R);
193  }
194 
195  static bool add(Integral A, Integral B, unsigned OpBits, Integral *R) {
196  return CheckAddUB(A.V, B.V, R->V);
197  }
198 
199  static bool sub(Integral A, Integral B, unsigned OpBits, Integral *R) {
200  return CheckSubUB(A.V, B.V, R->V);
201  }
202 
203  static bool mul(Integral A, Integral B, unsigned OpBits, Integral *R) {
204  return CheckMulUB(A.V, B.V, R->V);
205  }
206 
207 private:
208  template <typename T>
209  static typename std::enable_if<std::is_signed<T>::value, bool>::type
210  CheckAddUB(T A, T B, T &R) {
211  return llvm::AddOverflow<T>(A, B, R);
212  }
213 
214  template <typename T>
215  static typename std::enable_if<std::is_unsigned<T>::value, bool>::type
216  CheckAddUB(T A, T B, T &R) {
217  R = A + B;
218  return false;
219  }
220 
221  template <typename T>
222  static typename std::enable_if<std::is_signed<T>::value, bool>::type
223  CheckSubUB(T A, T B, T &R) {
224  return llvm::SubOverflow<T>(A, B, R);
225  }
226 
227  template <typename T>
228  static typename std::enable_if<std::is_unsigned<T>::value, bool>::type
229  CheckSubUB(T A, T B, T &R) {
230  R = A - B;
231  return false;
232  }
233 
234  template <typename T>
235  static typename std::enable_if<std::is_signed<T>::value, bool>::type
236  CheckMulUB(T A, T B, T &R) {
237  return llvm::MulOverflow<T>(A, B, R);
238  }
239 
240  template <typename T>
241  static typename std::enable_if<std::is_unsigned<T>::value, bool>::type
242  CheckMulUB(T A, T B, T &R) {
243  R = A * B;
244  return false;
245  }
246 
247  template <typename T, T Min, T Max>
248  static typename std::enable_if<std::is_signed<T>::value, bool>::type
249  CheckRange(int64_t V) {
250  return Min <= V && V <= Max;
251  }
252 
253  template <typename T, T Min, T Max>
254  static typename std::enable_if<std::is_unsigned<T>::value, bool>::type
255  CheckRange(int64_t V) {
256  return V >= 0 && static_cast<uint64_t>(V) <= Max;
257  }
258 };
259 
260 template <unsigned Bits, bool Signed>
261 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, Integral<Bits, Signed> I) {
262  I.print(OS);
263  return OS;
264 }
265 
266 } // namespace interp
267 } // namespace clang
268 
269 #endif
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
static bool mul(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:203
bool operator!=(Integral RHS) const
Definition: Integral.h:88
Wrapper around numeric types.
Definition: Integral.h:56
bool isMin() const
Definition: Integral.h:125
APValue toAPValue() const
Definition: Integral.h:115
ComparisonCategoryResult compare(const Integral &RHS) const
Definition: Integral.h:134
Integral operator~() const
Definition: Integral.h:95
static Integral max(unsigned NumBits)
Definition: Integral.h:154
static bool increment(Integral A, Integral *R)
Definition: Integral.h:187
bool isZero() const
Definition: Integral.h:123
Integral< Bits, false > toUnsigned() const
Definition: Integral.h:117
__DEVICE__ int max(int __a, int __b)
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
static bool sub(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:199
static Integral zero()
Definition: Integral.h:177
bool operator<(Integral RHS) const
Definition: Integral.h:83
Integral truncate(unsigned TruncBits) const
Definition: Integral.h:140
bool isPositive() const
Definition: Integral.h:132
bool operator<=(Integral RHS) const
Definition: Integral.h:85
#define V(N, I)
Definition: ASTContext.h:2921
llvm::APSInt APSInt
Definition: Integral.h:28
bool operator==(Integral RHS) const
Definition: Integral.h:87
static Integral from(Integral< 0, SrcSign > Value)
Definition: Integral.h:170
Integral(const APSInt &V)
Construct an integral from a value based on signedness.
Definition: Integral.h:80
static bool inRange(int64_t Value, unsigned NumBits)
Definition: Integral.h:183
static bool decrement(Integral A, Integral *R)
Definition: Integral.h:191
static std::enable_if< SrcBits !=0, Integral >::type from(Integral< SrcBits, SrcSign > Value)
Definition: Integral.h:166
#define false
Definition: stdbool.h:17
static Integral min(unsigned NumBits)
Definition: Integral.h:151
static constexpr unsigned bitWidth()
Definition: Integral.h:121
void print(llvm::raw_ostream &OS) const
Definition: Integral.h:149
APSInt toAPSInt(unsigned NumBits) const
Definition: Integral.h:109
bool isNegative() const
Definition: Integral.h:131
Integral operator-() const
Definition: Integral.h:94
static bool add(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:195
llvm::APInt APInt
Definition: Integral.h:27
Integral(Integral< SrcBits, SrcSign > V)
Constructs an integral from another integral.
Definition: Integral.h:77
ComparisonCategoryResult Compare(const T &X, const T &Y)
Helper to compare two comparable types.
Definition: Integral.h:32
Dataflow Directional Tag Classes.
bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is in range.
Definition: Interp.cpp:240
bool operator>=(Integral RHS) const
Definition: Integral.h:86
unsigned countLeadingZeros() const
Definition: Integral.h:138
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat]...
Definition: APValue.h:115
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:14595
__DEVICE__ int min(int __a, int __b)
static constexpr bool isSigned()
Definition: Integral.h:129
bool operator>(unsigned RHS) const
Definition: Integral.h:90
Integral()
Zero-initializes an integral.
Definition: Integral.h:73
bool isMinusOne() const
Definition: Integral.h:127
APSInt toAPSInt() const
Definition: Integral.h:106
static std::enable_if< std::is_integral< T >::value, Integral >::type from(T Value)
Definition: Integral.h:160
#define true
Definition: stdbool.h:16
static Integral from(T Value, unsigned NumBits)
Definition: Integral.h:179
bool operator>(Integral RHS) const
Definition: Integral.h:84