xref: /openbsd-src/gnu/llvm/clang/lib/AST/Interp/Boolean.h (revision 12c855180aad702bbcca06e0398d774beeafb155)
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 <cstddef>
13 #include <cstdint>
14 #include "Integral.h"
15 #include "clang/AST/APValue.h"
16 #include "clang/AST/ComparisonCategories.h"
17 #include "llvm/ADT/APSInt.h"
18 #include "llvm/Support/MathExtras.h"
19 #include "llvm/Support/raw_ostream.h"
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   /// Construct a wrapper from a boolean.
Boolean(bool V)31   explicit Boolean(bool V) : V(V) {}
32 
33  public:
34   /// Zero-initializes a boolean.
Boolean()35   Boolean() : V(false) {}
36 
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   bool operator==(Boolean RHS) const { return V == RHS.V; }
42   bool operator!=(Boolean RHS) const { return V != RHS.V; }
43 
44   bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }
45 
46   Boolean operator-() const { return Boolean(V); }
47   Boolean operator~() const { return Boolean(true); }
48 
int8_t()49   explicit operator int8_t() const { return V; }
uint8_t()50   explicit operator uint8_t() const { return V; }
int16_t()51   explicit operator int16_t() const { return V; }
uint16_t()52   explicit operator uint16_t() const { return V; }
int32_t()53   explicit operator int32_t() const { return V; }
uint32_t()54   explicit operator uint32_t() const { return V; }
int64_t()55   explicit operator int64_t() const { return V; }
uint64_t()56   explicit operator uint64_t() const { return V; }
57   explicit operator bool() const { return V; }
58 
toAPSInt()59   APSInt toAPSInt() const {
60     return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
61   }
toAPSInt(unsigned NumBits)62   APSInt toAPSInt(unsigned NumBits) const {
63     return APSInt(toAPSInt().zextOrTrunc(NumBits), true);
64   }
toAPValue()65   APValue toAPValue() const { return APValue(toAPSInt()); }
66 
toUnsigned()67   Boolean toUnsigned() const { return *this; }
68 
bitWidth()69   constexpr static unsigned bitWidth() { return true; }
isZero()70   bool isZero() const { return !V; }
isMin()71   bool isMin() const { return isZero(); }
72 
isMinusOne()73   constexpr static bool isMinusOne() { return false; }
74 
isSigned()75   constexpr static bool isSigned() { return false; }
76 
isNegative()77   constexpr static bool isNegative() { return false; }
isPositive()78   constexpr static bool isPositive() { return !isNegative(); }
79 
compare(const Boolean & RHS)80   ComparisonCategoryResult compare(const Boolean &RHS) const {
81     return Compare(V, RHS.V);
82   }
83 
countLeadingZeros()84   unsigned countLeadingZeros() const { return V ? 0 : 1; }
85 
truncate(unsigned TruncBits)86   Boolean truncate(unsigned TruncBits) const { return *this; }
87 
print(llvm::raw_ostream & OS)88   void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
89 
min(unsigned NumBits)90   static Boolean min(unsigned NumBits) { return Boolean(false); }
max(unsigned NumBits)91   static Boolean max(unsigned NumBits) { return Boolean(true); }
92 
from(T Value)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>
from(Integral<SrcBits,SrcSign> Value)101   from(Integral<SrcBits, SrcSign> Value) {
102     return Boolean(!Value.isZero());
103   }
104 
105   template <bool SrcSign>
from(Integral<0,SrcSign> Value)106   static Boolean from(Integral<0, SrcSign> Value) {
107     return Boolean(!Value.isZero());
108   }
109 
zero()110   static Boolean zero() { return from(false); }
111 
112   template <typename T>
from(T Value,unsigned NumBits)113   static Boolean from(T Value, unsigned NumBits) {
114     return Boolean(Value);
115   }
116 
inRange(int64_t Value,unsigned NumBits)117   static bool inRange(int64_t Value, unsigned NumBits) {
118     return Value == 0 || Value == 1;
119   }
120 
increment(Boolean A,Boolean * R)121   static bool increment(Boolean A, Boolean *R) {
122     *R = Boolean(true);
123     return false;
124   }
125 
decrement(Boolean A,Boolean * R)126   static bool decrement(Boolean A, Boolean *R) {
127     llvm_unreachable("Cannot decrement booleans");
128   }
129 
add(Boolean A,Boolean B,unsigned OpBits,Boolean * R)130   static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
131     *R = Boolean(A.V || B.V);
132     return false;
133   }
134 
sub(Boolean A,Boolean B,unsigned OpBits,Boolean * R)135   static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
136     *R = Boolean(A.V ^ B.V);
137     return false;
138   }
139 
mul(Boolean A,Boolean B,unsigned OpBits,Boolean * R)140   static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
141     *R = Boolean(A.V && B.V);
142     return false;
143   }
144 
inv(Boolean A,Boolean * R)145   static bool inv(Boolean A, Boolean *R) {
146     *R = Boolean(!A.V);
147     return false;
148   }
149 
neg(Boolean A,Boolean * R)150   static bool neg(Boolean A, Boolean *R) {
151     *R = Boolean(A.V);
152     return false;
153   }
154 };
155 
156 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {
157   B.print(OS);
158   return OS;
159 }
160 
161 }  // namespace interp
162 }  // namespace clang
163 
164 #endif
165