1 //===--- Boolean.h - Wrapper for boolean types for the VM -------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_AST_INTERP_BOOLEAN_H
10 #define LLVM_CLANG_AST_INTERP_BOOLEAN_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"
24 /// Wrapper around boolean types.
27 /// Underlying boolean.
31 /// Zero-initializes a boolean.
32 Boolean() : V(false) {}
33 explicit Boolean(bool V
) : V(V
) {}
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
; }
42 bool operator>(unsigned RHS
) const { return static_cast<unsigned>(V
) > RHS
; }
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); }
48 explicit operator int8_t() const { return V
; }
49 explicit operator uint8_t() const { return V
; }
50 explicit operator int16_t() const { return V
; }
51 explicit operator uint16_t() const { return V
; }
52 explicit operator int32_t() const { return V
; }
53 explicit operator uint32_t() const { return V
; }
54 explicit operator int64_t() const { return V
; }
55 explicit operator uint64_t() const { return V
; }
56 explicit operator bool() const { return V
; }
58 APSInt
toAPSInt() const {
59 return APSInt(APInt(1, static_cast<uint64_t>(V
), false), true);
61 APSInt
toAPSInt(unsigned NumBits
) const {
62 return APSInt(toAPSInt().zextOrTrunc(NumBits
), true);
64 APValue
toAPValue() const { return APValue(toAPSInt()); }
66 Boolean
toUnsigned() const { return *this; }
68 constexpr static unsigned bitWidth() { return 1; }
69 bool isZero() const { return !V
; }
70 bool isMin() const { return isZero(); }
72 constexpr static bool isMinusOne() { return false; }
74 constexpr static bool isSigned() { return false; }
76 constexpr static bool isNegative() { return false; }
77 constexpr static bool isPositive() { return !isNegative(); }
79 ComparisonCategoryResult
compare(const Boolean
&RHS
) const {
80 return Compare(V
, RHS
.V
);
83 unsigned countLeadingZeros() const { return V
? 0 : 1; }
85 Boolean
truncate(unsigned TruncBits
) const { return *this; }
87 void print(llvm::raw_ostream
&OS
) const { OS
<< (V
? "true" : "false"); }
88 std::string
toDiagnosticString(const ASTContext
&Ctx
) const {
90 llvm::raw_string_ostream
OS(NameStr
);
95 static Boolean
min(unsigned NumBits
) { return Boolean(false); }
96 static Boolean
max(unsigned NumBits
) { return Boolean(true); }
98 template <typename T
> static Boolean
from(T Value
) {
99 if constexpr (std::is_integral
<T
>::value
)
100 return Boolean(Value
!= 0);
101 return Boolean(static_cast<decltype(Boolean::V
)>(Value
) != 0);
104 template <unsigned SrcBits
, bool SrcSign
>
105 static std::enable_if_t
<SrcBits
!= 0, Boolean
>
106 from(Integral
<SrcBits
, SrcSign
> Value
) {
107 return Boolean(!Value
.isZero());
110 static Boolean
zero() { return from(false); }
112 template <typename T
>
113 static Boolean
from(T Value
, unsigned NumBits
) {
114 return Boolean(Value
);
117 static bool inRange(int64_t Value
, unsigned NumBits
) {
118 return Value
== 0 || Value
== 1;
121 static bool increment(Boolean A
, Boolean
*R
) {
126 static bool decrement(Boolean A
, Boolean
*R
) {
127 llvm_unreachable("Cannot decrement booleans");
130 static bool add(Boolean A
, Boolean B
, unsigned OpBits
, Boolean
*R
) {
131 *R
= Boolean(A
.V
|| B
.V
);
135 static bool sub(Boolean A
, Boolean B
, unsigned OpBits
, Boolean
*R
) {
136 *R
= Boolean(A
.V
^ B
.V
);
140 static bool mul(Boolean A
, Boolean B
, unsigned OpBits
, Boolean
*R
) {
141 *R
= Boolean(A
.V
&& B
.V
);
145 static bool inv(Boolean A
, Boolean
*R
) {
150 static bool neg(Boolean A
, Boolean
*R
) {
156 inline llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
, const Boolean
&B
) {
161 } // namespace interp