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;
50template <
unsigned Bits,
bool Signed>
class Integral final {
52 template <
unsigned OtherBits,
bool OtherSigned>
friend class Integral;
59 static const auto Min = std::numeric_limits<ReprT>::min();
60 static const auto Max = std::numeric_limits<ReprT>::max();
72 template <
unsigned SrcBits,
bool SrcSign>
77 :
V(
V.
isSigned() ?
V.getSExtValue() :
V.getZExtValue()) {}
87 return V >= 0 &&
static_cast<unsigned>(
V) > RHS;
96 template <
unsigned DstBits,
bool DstSign>
101 template <
typename Ty,
typename = std::enable_if_t<std::is_
integral_v<Ty>>>
102 explicit operator Ty()
const {
121 constexpr static unsigned bitWidth() {
return Bits; }
140 llvm::raw_string_ostream OS(NameStr);
147 return llvm::countl_zero<ReprT>(
V);
148 llvm_unreachable(
"Don't call countLeadingZeros() on signed types.");
152 if (TruncBits >= Bits)
154 const ReprT BitMask = (ReprT(1) << ReprT(TruncBits)) - 1;
155 const ReprT SignBit = ReprT(1) << (TruncBits - 1);
156 const ReprT ExtMask = ~BitMask;
160 void print(llvm::raw_ostream &OS)
const { OS <<
V; }
170 if constexpr (std::is_integral<ValT>::value)
176 template <
unsigned SrcBits,
bool SrcSign>
177 static std::enable_if_t<SrcBits != 0, Integral>
189 return CheckRange<ReprT, Min, Max>(
Value);
201 return CheckAddUB(A.V, B.V, R->V);
205 return CheckSubUB(A.V, B.V, R->V);
209 return CheckMulUB(A.V, B.V, R->V);
250 template <
unsigned RHSBits,
bool RHSSign>
256 template <
unsigned RHSBits,
bool RHSSign>
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);
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);
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);
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;
293 return V >= 0 &&
static_cast<uint64_t>(
V) <= Max;
298template <
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.