Path: blob/main/contrib/llvm-project/clang/lib/AST/ByteCode/Boolean.h
213799 views
//===--- Boolean.h - Wrapper for boolean types for the VM -------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_CLANG_AST_INTERP_BOOLEAN_H9#define LLVM_CLANG_AST_INTERP_BOOLEAN_H1011#include "Integral.h"12#include "clang/AST/APValue.h"13#include "clang/AST/ComparisonCategories.h"14#include "llvm/ADT/APSInt.h"15#include "llvm/Support/MathExtras.h"16#include "llvm/Support/raw_ostream.h"17#include <cstddef>18#include <cstdint>1920namespace clang {21namespace interp {2223/// Wrapper around boolean types.24class Boolean final {25private:26/// Underlying boolean.27bool V;2829public:30/// Zero-initializes a boolean.31Boolean() : V(false) {}32explicit Boolean(bool V) : V(V) {}3334bool operator<(Boolean RHS) const { return V < RHS.V; }35bool operator>(Boolean RHS) const { return V > RHS.V; }36bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }3738Boolean operator-() const { return Boolean(V); }39Boolean operator-(const Boolean &Other) const { return Boolean(V - Other.V); }40Boolean operator~() const { return Boolean(true); }41Boolean operator!() const { return Boolean(!V); }4243template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>>44explicit operator Ty() const {45return V;46}4748APSInt toAPSInt() const {49return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);50}51APSInt toAPSInt(unsigned NumBits) const {52return APSInt(toAPSInt().zextOrTrunc(NumBits), true);53}54APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); }5556Boolean toUnsigned() const { return *this; }5758constexpr static unsigned bitWidth() { return 1; }59bool isZero() const { return !V; }60bool isMin() const { return isZero(); }6162constexpr static bool isMinusOne() { return false; }6364constexpr static bool isSigned() { return false; }6566constexpr static bool isNegative() { return false; }67constexpr static bool isPositive() { return !isNegative(); }6869ComparisonCategoryResult compare(const Boolean &RHS) const {70return Compare(V, RHS.V);71}7273unsigned countLeadingZeros() const { return V ? 0 : 1; }7475Boolean truncate(unsigned TruncBits) const { return *this; }7677static Boolean bitcastFromMemory(const std::byte *Buff, unsigned BitWidth) {78// Just load the first byte.79bool Val = static_cast<bool>(*Buff);80return Boolean(Val);81}8283void bitcastToMemory(std::byte *Buff) { std::memcpy(Buff, &V, sizeof(V)); }8485void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }86std::string toDiagnosticString(const ASTContext &Ctx) const {87std::string NameStr;88llvm::raw_string_ostream OS(NameStr);89print(OS);90return NameStr;91}9293static Boolean min(unsigned NumBits) { return Boolean(false); }94static Boolean max(unsigned NumBits) { return Boolean(true); }9596template <typename T> static Boolean from(T Value) {97if constexpr (std::is_integral<T>::value)98return Boolean(Value != 0);99return Boolean(static_cast<decltype(Boolean::V)>(Value) != 0);100}101102template <unsigned SrcBits, bool SrcSign>103static std::enable_if_t<SrcBits != 0, Boolean>104from(Integral<SrcBits, SrcSign> Value) {105return Boolean(!Value.isZero());106}107108static Boolean zero() { return from(false); }109110template <typename T> static Boolean from(T Value, unsigned NumBits) {111return Boolean(Value);112}113114static bool inRange(int64_t Value, unsigned NumBits) {115return Value == 0 || Value == 1;116}117118static bool increment(Boolean A, Boolean *R) {119*R = Boolean(true);120return false;121}122123static bool decrement(Boolean A, Boolean *R) {124llvm_unreachable("Cannot decrement booleans");125}126127static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {128*R = Boolean(A.V || B.V);129return false;130}131132static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {133*R = Boolean(A.V ^ B.V);134return false;135}136137static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {138*R = Boolean(A.V && B.V);139return false;140}141142static bool inv(Boolean A, Boolean *R) {143*R = Boolean(!A.V);144return false;145}146147static bool neg(Boolean A, Boolean *R) {148*R = Boolean(A.V);149return false;150}151};152153inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {154B.print(OS);155return OS;156}157158} // namespace interp159} // namespace clang160161#endif162163164