xref: /freebsd/contrib/llvm-project/clang/lib/AST/ByteCode/Boolean.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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.
Boolean()32   Boolean() : V(false) {}
Boolean(bool V)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>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }
38 
39   Boolean operator-() const { return Boolean(V); }
40   Boolean operator-(const Boolean &Other) const { return Boolean(V - Other.V); }
41   Boolean operator~() const { return Boolean(true); }
42   Boolean operator!() const { return Boolean(!V); }
43 
44   template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>>
Ty()45   explicit operator Ty() const {
46     return V;
47   }
48 
toAPSInt()49   APSInt toAPSInt() const {
50     return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
51   }
toAPSInt(unsigned NumBits)52   APSInt toAPSInt(unsigned NumBits) const {
53     return APSInt(toAPSInt().zextOrTrunc(NumBits), true);
54   }
toAPValue(const ASTContext &)55   APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); }
56 
toUnsigned()57   Boolean toUnsigned() const { return *this; }
58 
bitWidth()59   constexpr static unsigned bitWidth() { return 1; }
isZero()60   bool isZero() const { return !V; }
isMin()61   bool isMin() const { return isZero(); }
62 
isMinusOne()63   constexpr static bool isMinusOne() { return false; }
64 
isSigned()65   constexpr static bool isSigned() { return false; }
66 
isNegative()67   constexpr static bool isNegative() { return false; }
isPositive()68   constexpr static bool isPositive() { return !isNegative(); }
69 
compare(const Boolean & RHS)70   ComparisonCategoryResult compare(const Boolean &RHS) const {
71     return Compare(V, RHS.V);
72   }
73 
countLeadingZeros()74   unsigned countLeadingZeros() const { return V ? 0 : 1; }
75 
truncate(unsigned TruncBits)76   Boolean truncate(unsigned TruncBits) const { return *this; }
77 
bitcastFromMemory(const std::byte * Buff,unsigned BitWidth)78   static Boolean bitcastFromMemory(const std::byte *Buff, unsigned BitWidth) {
79     // Just load the first byte.
80     bool Val = static_cast<bool>(*Buff);
81     return Boolean(Val);
82   }
83 
bitcastToMemory(std::byte * Buff)84   void bitcastToMemory(std::byte *Buff) { std::memcpy(Buff, &V, sizeof(V)); }
85 
print(llvm::raw_ostream & OS)86   void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
toDiagnosticString(const ASTContext & Ctx)87   std::string toDiagnosticString(const ASTContext &Ctx) const {
88     std::string NameStr;
89     llvm::raw_string_ostream OS(NameStr);
90     print(OS);
91     return NameStr;
92   }
93 
min(unsigned NumBits)94   static Boolean min(unsigned NumBits) { return Boolean(false); }
max(unsigned NumBits)95   static Boolean max(unsigned NumBits) { return Boolean(true); }
96 
from(T Value)97   template <typename T> static Boolean from(T Value) {
98     if constexpr (std::is_integral<T>::value)
99       return Boolean(Value != 0);
100     return Boolean(static_cast<decltype(Boolean::V)>(Value) != 0);
101   }
102 
103   template <unsigned SrcBits, bool SrcSign>
104   static std::enable_if_t<SrcBits != 0, Boolean>
from(Integral<SrcBits,SrcSign> Value)105   from(Integral<SrcBits, SrcSign> Value) {
106     return Boolean(!Value.isZero());
107   }
108 
zero()109   static Boolean zero() { return from(false); }
110 
from(T Value,unsigned NumBits)111   template <typename T> static Boolean from(T Value, unsigned NumBits) {
112     return Boolean(Value);
113   }
114 
inRange(int64_t Value,unsigned NumBits)115   static bool inRange(int64_t Value, unsigned NumBits) {
116     return Value == 0 || Value == 1;
117   }
118 
increment(Boolean A,Boolean * R)119   static bool increment(Boolean A, Boolean *R) {
120     *R = Boolean(true);
121     return false;
122   }
123 
decrement(Boolean A,Boolean * R)124   static bool decrement(Boolean A, Boolean *R) {
125     llvm_unreachable("Cannot decrement booleans");
126   }
127 
add(Boolean A,Boolean B,unsigned OpBits,Boolean * R)128   static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
129     *R = Boolean(A.V || B.V);
130     return false;
131   }
132 
sub(Boolean A,Boolean B,unsigned OpBits,Boolean * R)133   static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
134     *R = Boolean(A.V ^ B.V);
135     return false;
136   }
137 
mul(Boolean A,Boolean B,unsigned OpBits,Boolean * R)138   static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
139     *R = Boolean(A.V && B.V);
140     return false;
141   }
142 
inv(Boolean A,Boolean * R)143   static bool inv(Boolean A, Boolean *R) {
144     *R = Boolean(!A.V);
145     return false;
146   }
147 
neg(Boolean A,Boolean * R)148   static bool neg(Boolean A, Boolean *R) {
149     *R = Boolean(A.V);
150     return false;
151   }
152 };
153 
154 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {
155   B.print(OS);
156   return OS;
157 }
158 
159 } // namespace interp
160 } // namespace clang
161 
162 #endif
163