13#ifndef LLVM_CLANG_AST_INTERP_INTEGRAL_AP_H
14#define LLVM_CLANG_AST_INTERP_INTEGRAL_AP_H
18#include "llvm/ADT/APSInt.h"
19#include "llvm/Support/MathExtras.h"
20#include "llvm/Support/raw_ostream.h"
29using APInt = llvm::APInt;
30using APSInt = llvm::APSInt;
31template <
unsigned Bits,
bool Signed>
class Integral;
38 template <
typename T,
bool InputSigned>
39 static T truncateCast(
const APInt &
V) {
40 constexpr unsigned BitSize =
sizeof(
T) * 8;
41 if (BitSize >=
V.getBitWidth()) {
43 if constexpr (InputSigned)
44 Extended =
V.sext(BitSize);
46 Extended =
V.zext(BitSize);
47 return std::is_signed_v<T> ? Extended.getSExtValue()
48 : Extended.getZExtValue();
51 return std::is_signed_v<T> ?
V.trunc(BitSize).getSExtValue()
52 :
V.trunc(BitSize).getZExtValue();
91 template <
typename Ty,
typename = std::enable_if_t<std::is_
integral_v<Ty>>>
92 explicit operator Ty()
const {
93 return truncateCast<Ty, Signed>(
V);
103 template <
bool InputSigned>
106 NumBits =
V.bitWidth();
108 if constexpr (InputSigned)
113 template <
unsigned Bits,
bool InputSigned>
115 APInt Copy =
APInt(BitWidth,
static_cast<uint64_t
>(I), InputSigned);
125 constexpr unsigned bitWidth()
const {
return V.getBitWidth(); }
141 bool isMin()
const {
return V.isMinValue(); }
142 bool isMax()
const {
return V.isMaxValue(); }
148 void print(llvm::raw_ostream &OS)
const { OS <<
V; }
151 llvm::raw_string_ostream OS(NameStr);
160 return IntegralAP(V.trunc(BitWidth).zextOrTrunc(this->bitWidth()));
198 return CheckAddSubMulUB<std::plus>(A, B, OpBits, R);
202 return CheckAddSubMulUB<std::minus>(A, B, OpBits, R);
206 return CheckAddSubMulUB<std::multiplies>(A, B, OpBits, R);
262 unsigned ShiftAmount = B.V.getZExtValue();
272 return sizeof(uint32_t) + (
V.getBitWidth() /
CHAR_BIT);
276 assert(
V.getBitWidth() < std::numeric_limits<uint8_t>::max());
277 uint32_t BitWidth =
V.getBitWidth();
279 std::memcpy(Buff, &BitWidth,
sizeof(uint32_t));
280 llvm::StoreIntToMemory(
V, (uint8_t *)(Buff +
sizeof(uint32_t)),
286 std::memcpy(&BitWidth, Buff,
sizeof(uint32_t));
289 llvm::LoadIntFromMemory(Val.V, (
const uint8_t *)Buff +
sizeof(uint32_t),
295 template <
template <
typename T>
class Op>
299 R->V = Op<APInt>{}(A.V, B.V);
305 APSInt Value = Op<APSInt>{}(LHS.extend(BitWidth), RHS.extend(BitWidth));
313template <
bool Signed>
320template <
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 ...
static IntegralAP< Signed > deserialize(const std::byte *Buff)
void print(llvm::raw_ostream &OS) const
static constexpr bool isSigned()
static void shiftLeft(const IntegralAP A, const IntegralAP B, unsigned OpBits, IntegralAP *R)
static IntegralAP zero(int32_t BitWidth)
bool operator<=(IntegralAP RHS) const
IntegralAP< false > toUnsigned() const
static IntegralAP from(IntegralAP< InputSigned > V, unsigned NumBits=0)
static bool rem(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R)
void serialize(std::byte *Buff) const
static bool decrement(IntegralAP A, IntegralAP *R)
static bool increment(IntegralAP A, IntegralAP *R)
static bool bitOr(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R)
static void shiftRight(const IntegralAP A, const IntegralAP B, unsigned OpBits, IntegralAP *R)
IntegralAP operator-(const IntegralAP &Other) const
bool operator>(const IntegralAP &RHS) const
APValue toAPValue(const ASTContext &) const
std::string toDiagnosticString(const ASTContext &Ctx) const
IntegralAP(T Value, unsigned BitWidth)
APSInt toAPSInt(unsigned Bits=0) const
static IntegralAP from(T Value, unsigned NumBits=0)
ComparisonCategoryResult compare(const IntegralAP &RHS) const
IntegralAP truncate(unsigned BitWidth) const
constexpr unsigned bitWidth() const
static bool add(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R)
static bool bitXor(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R)
IntegralAP operator-() const
size_t bytesToSerialize() const
static bool div(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R)
static bool sub(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R)
static bool mul(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R)
bool operator>=(IntegralAP RHS) const
unsigned countLeadingZeros() const
static bool comp(IntegralAP A, IntegralAP *R)
bool operator<(IntegralAP RHS) const
static IntegralAP from(Integral< Bits, InputSigned > I, unsigned BitWidth)
IntegralAP()
Arbitrary value for uninitialized variables.
static bool bitAnd(IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R)
static bool neg(const IntegralAP &A, IntegralAP *R)
Wrapper around numeric types.
Floating getSwappedBytes(Floating F)
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
The JSON file list parser is used to communicate input to InstallAPI.
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
@ Result
The result type of a method or function.
const FunctionProtoType * T
@ Other
Other implicit parameter.