1 //===--- Boolean.h - Wrapper for boolean types for the VM -------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_CLANG_AST_INTERP_BOOLEAN_H 10 #define LLVM_CLANG_AST_INTERP_BOOLEAN_H 11 12 #include "Integral.h" 13 #include "clang/AST/APValue.h" 14 #include "clang/AST/ComparisonCategories.h" 15 #include "llvm/ADT/APSInt.h" 16 #include "llvm/Support/MathExtras.h" 17 #include "llvm/Support/raw_ostream.h" 18 #include <cstddef> 19 #include <cstdint> 20 21 namespace clang { 22 namespace interp { 23 24 /// Wrapper around boolean types. 25 class Boolean final { 26 private: 27 /// Underlying boolean. 28 bool V; 29 30 public: 31 /// Zero-initializes a boolean. 32 Boolean() : V(false) {} 33 explicit Boolean(bool V) : V(V) {} 34 35 bool operator<(Boolean RHS) const { return V < RHS.V; } 36 bool operator>(Boolean RHS) const { return V > RHS.V; } 37 bool operator<=(Boolean RHS) const { return V <= RHS.V; } 38 bool operator>=(Boolean RHS) const { return V >= RHS.V; } 39 bool operator==(Boolean RHS) const { return V == RHS.V; } 40 bool operator!=(Boolean RHS) const { return V != RHS.V; } 41 42 bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; } 43 44 Boolean operator-() const { return Boolean(V); } 45 Boolean operator-(const Boolean &Other) const { return Boolean(V - Other.V); } 46 Boolean operator~() const { return Boolean(true); } 47 48 template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>> 49 explicit operator Ty() const { 50 return V; 51 } 52 53 APSInt toAPSInt() const { 54 return APSInt(APInt(1, static_cast<uint64_t>(V), false), true); 55 } 56 APSInt toAPSInt(unsigned NumBits) const { 57 return APSInt(toAPSInt().zextOrTrunc(NumBits), true); 58 } 59 APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); } 60 61 Boolean toUnsigned() const { return *this; } 62 63 constexpr static unsigned bitWidth() { return 1; } 64 bool isZero() const { return !V; } 65 bool isMin() const { return isZero(); } 66 67 constexpr static bool isMinusOne() { return false; } 68 69 constexpr static bool isSigned() { return false; } 70 71 constexpr static bool isNegative() { return false; } 72 constexpr static bool isPositive() { return !isNegative(); } 73 74 ComparisonCategoryResult compare(const Boolean &RHS) const { 75 return Compare(V, RHS.V); 76 } 77 78 unsigned countLeadingZeros() const { return V ? 0 : 1; } 79 80 Boolean truncate(unsigned TruncBits) const { return *this; } 81 82 void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); } 83 std::string toDiagnosticString(const ASTContext &Ctx) const { 84 std::string NameStr; 85 llvm::raw_string_ostream OS(NameStr); 86 print(OS); 87 return NameStr; 88 } 89 90 static Boolean min(unsigned NumBits) { return Boolean(false); } 91 static Boolean max(unsigned NumBits) { return Boolean(true); } 92 93 template <typename T> static Boolean from(T Value) { 94 if constexpr (std::is_integral<T>::value) 95 return Boolean(Value != 0); 96 return Boolean(static_cast<decltype(Boolean::V)>(Value) != 0); 97 } 98 99 template <unsigned SrcBits, bool SrcSign> 100 static std::enable_if_t<SrcBits != 0, Boolean> 101 from(Integral<SrcBits, SrcSign> Value) { 102 return Boolean(!Value.isZero()); 103 } 104 105 static Boolean zero() { return from(false); } 106 107 template <typename T> static Boolean from(T Value, unsigned NumBits) { 108 return Boolean(Value); 109 } 110 111 static bool inRange(int64_t Value, unsigned NumBits) { 112 return Value == 0 || Value == 1; 113 } 114 115 static bool increment(Boolean A, Boolean *R) { 116 *R = Boolean(true); 117 return false; 118 } 119 120 static bool decrement(Boolean A, Boolean *R) { 121 llvm_unreachable("Cannot decrement booleans"); 122 } 123 124 static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) { 125 *R = Boolean(A.V || B.V); 126 return false; 127 } 128 129 static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) { 130 *R = Boolean(A.V ^ B.V); 131 return false; 132 } 133 134 static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) { 135 *R = Boolean(A.V && B.V); 136 return false; 137 } 138 139 static bool inv(Boolean A, Boolean *R) { 140 *R = Boolean(!A.V); 141 return false; 142 } 143 144 static bool neg(Boolean A, Boolean *R) { 145 *R = Boolean(A.V); 146 return false; 147 } 148 }; 149 150 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) { 151 B.print(OS); 152 return OS; 153 } 154 155 } // namespace interp 156 } // namespace clang 157 158 #endif 159