Path: blob/main/contrib/llvm-project/clang/lib/AST/Interp/Boolean.h
35292 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 <cstddef>12#include <cstdint>13#include "Integral.h"14#include "clang/AST/APValue.h"15#include "clang/AST/ComparisonCategories.h"16#include "llvm/ADT/APSInt.h"17#include "llvm/Support/MathExtras.h"18#include "llvm/Support/raw_ostream.h"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<=(Boolean RHS) const { return V <= RHS.V; }37bool operator>=(Boolean RHS) const { return V >= RHS.V; }38bool operator==(Boolean RHS) const { return V == RHS.V; }39bool operator!=(Boolean RHS) const { return V != RHS.V; }4041bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }4243Boolean operator-() const { return Boolean(V); }44Boolean operator-(const Boolean &Other) const { return Boolean(V - Other.V); }45Boolean operator~() const { return Boolean(true); }4647template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>>48explicit operator Ty() const {49return V;50}5152APSInt toAPSInt() const {53return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);54}55APSInt toAPSInt(unsigned NumBits) const {56return APSInt(toAPSInt().zextOrTrunc(NumBits), true);57}58APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); }5960Boolean toUnsigned() const { return *this; }6162constexpr static unsigned bitWidth() { return 1; }63bool isZero() const { return !V; }64bool isMin() const { return isZero(); }6566constexpr static bool isMinusOne() { return false; }6768constexpr static bool isSigned() { return false; }6970constexpr static bool isNegative() { return false; }71constexpr static bool isPositive() { return !isNegative(); }7273ComparisonCategoryResult compare(const Boolean &RHS) const {74return Compare(V, RHS.V);75}7677unsigned countLeadingZeros() const { return V ? 0 : 1; }7879Boolean truncate(unsigned TruncBits) const { return *this; }8081void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }82std::string toDiagnosticString(const ASTContext &Ctx) const {83std::string NameStr;84llvm::raw_string_ostream OS(NameStr);85print(OS);86return NameStr;87}8889static Boolean min(unsigned NumBits) { return Boolean(false); }90static Boolean max(unsigned NumBits) { return Boolean(true); }9192template <typename T> static Boolean from(T Value) {93if constexpr (std::is_integral<T>::value)94return Boolean(Value != 0);95return Boolean(static_cast<decltype(Boolean::V)>(Value) != 0);96}9798template <unsigned SrcBits, bool SrcSign>99static std::enable_if_t<SrcBits != 0, Boolean>100from(Integral<SrcBits, SrcSign> Value) {101return Boolean(!Value.isZero());102}103104static Boolean zero() { return from(false); }105106template <typename T>107static Boolean from(T Value, unsigned NumBits) {108return Boolean(Value);109}110111static bool inRange(int64_t Value, unsigned NumBits) {112return Value == 0 || Value == 1;113}114115static bool increment(Boolean A, Boolean *R) {116*R = Boolean(true);117return false;118}119120static bool decrement(Boolean A, Boolean *R) {121llvm_unreachable("Cannot decrement booleans");122}123124static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {125*R = Boolean(A.V || B.V);126return false;127}128129static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {130*R = Boolean(A.V ^ B.V);131return false;132}133134static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {135*R = Boolean(A.V && B.V);136return false;137}138139static bool inv(Boolean A, Boolean *R) {140*R = Boolean(!A.V);141return false;142}143144static bool neg(Boolean A, Boolean *R) {145*R = Boolean(A.V);146return false;147}148};149150inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {151B.print(OS);152return OS;153}154155} // namespace interp156} // namespace clang157158#endif159160161