13#ifndef LLVM_CLANG_AST_INTERP_INTEGRAL_H
14#define LLVM_CLANG_AST_INTERP_INTEGRAL_H
18#include "llvm/ADT/APSInt.h"
19#include "llvm/Support/MathExtras.h"
20#include "llvm/Support/raw_ostream.h"
30using APSInt = llvm::APSInt;
35template <
unsigned Bits,
bool Signed>
struct Repr;
66template <
unsigned Bits,
bool Signed>
class Integral final {
68 template <
unsigned OtherBits,
bool OtherSigned>
friend class Integral;
75 static const auto Min = std::numeric_limits<ReprT>::min();
76 static const auto Max = std::numeric_limits<ReprT>::max();
88 template <
unsigned SrcBits,
bool SrcSign>
93 :
V(
V.
isSigned() ?
V.getSExtValue() :
V.getZExtValue()) {}
103 return V >= 0 &&
static_cast<unsigned>(
V) > RHS;
112 template <
unsigned DstBits,
bool DstSign>
117 template <
typename Ty,
typename = std::enable_if_t<std::is_
integral_v<Ty>>>
118 explicit operator Ty()
const {
137 constexpr static unsigned bitWidth() {
return Bits; }
156 llvm::raw_string_ostream OS(NameStr);
163 return llvm::countl_zero<ReprT>(
V);
164 llvm_unreachable(
"Don't call countLeadingZeros() on signed types.");
168 if (TruncBits >= Bits)
170 const ReprT BitMask = (ReprT(1) << ReprT(TruncBits)) - 1;
171 const ReprT SignBit = ReprT(1) << (TruncBits - 1);
172 const ReprT ExtMask = ~BitMask;
176 void print(llvm::raw_ostream &OS)
const { OS <<
V; }
182 if constexpr (std::is_integral<ValT>::value)
188 template <
unsigned SrcBits,
bool SrcSign>
189 static std::enable_if_t<SrcBits != 0, Integral>
201 return CheckRange<ReprT, Min, Max>(
Value);
213 return CheckAddUB(A.V, B.V, R->V);
217 return CheckSubUB(A.V, B.V, R->V);
221 return CheckMulUB(A.V, B.V, R->V);
262 template <
unsigned RHSBits,
bool RHSSign>
268 template <
unsigned RHSBits,
bool RHSSign>
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);
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);
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);
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;
305 return V >= 0 &&
static_cast<uint64_t>(
V) <= Max;
310template <
unsigned Bits,
bool Signed>
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Wrapper around numeric types.
Integral< Bits, false > toUnsigned() const
static Integral max(unsigned NumBits)
Integral()
Zero-initializes an integral.
std::string toDiagnosticString(const ASTContext &Ctx) const
static bool mul(Integral A, Integral B, unsigned OpBits, Integral *R)
bool operator>=(Integral RHS) const
void print(llvm::raw_ostream &OS) const
static bool sub(Integral A, Integral B, unsigned OpBits, Integral *R)
static constexpr unsigned bitWidth()
static void shiftRight(const Integral A, const Integral< RHSBits, RHSSign > B, unsigned OpBits, Integral *R)
bool operator>(Integral RHS) const
bool operator>(unsigned RHS) const
Integral operator-(const Integral &Other) const
unsigned countLeadingZeros() const
Integral operator~() const
Integral truncate(unsigned TruncBits) const
bool operator<(Integral RHS) const
ComparisonCategoryResult compare(const Integral &RHS) const
static Integral from(ValT Value)
static bool neg(Integral A, Integral *R)
bool operator!=(Integral RHS) const
APValue toAPValue(const ASTContext &) const
static bool decrement(Integral A, Integral *R)
static bool comp(Integral A, Integral *R)
Integral(Integral< SrcBits, SrcSign > V)
Constructs an integral from another integral.
static Integral from(T Value, unsigned NumBits)
bool operator<=(Integral RHS) const
static bool div(Integral A, Integral B, unsigned OpBits, Integral *R)
static bool inRange(int64_t Value, unsigned NumBits)
APSInt toAPSInt(unsigned NumBits) const
static bool bitXor(Integral A, Integral B, unsigned OpBits, Integral *R)
static bool rem(Integral A, Integral B, unsigned OpBits, Integral *R)
bool operator==(Integral RHS) const
Integral(const APSInt &V)
Construct an integral from a value based on signedness.
static Integral min(unsigned NumBits)
static constexpr bool isSigned()
static void shiftLeft(const Integral A, const Integral< RHSBits, RHSSign > B, unsigned OpBits, Integral *R)
static bool bitOr(Integral A, Integral B, unsigned OpBits, Integral *R)
Integral operator-() const
static std::enable_if_t< SrcBits !=0, Integral > from(Integral< SrcBits, SrcSign > Value)
static bool add(Integral A, Integral B, unsigned OpBits, Integral *R)
static bool increment(Integral A, Integral *R)
static bool bitAnd(Integral A, Integral B, unsigned OpBits, Integral *R)
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
ComparisonCategoryResult Compare(const T &X, const T &Y)
Helper to compare two comparable types.
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.