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/Support/raw_ostream.h"
16#include <cstddef>
17#include <cstdint>
18
19namespace clang {
20namespace interp {
21
22/// Wrapper around boolean types.
23class Boolean final {
24private:
25 /// Underlying boolean.
26 bool V;
27
28public:
29 /// Zero-initializes a boolean.
30 Boolean() : V(false) {}
31 explicit Boolean(bool V) : V(V) {}
32
33 bool operator<(Boolean RHS) const { return V < RHS.V; }
34 bool operator>(Boolean RHS) const { return V > RHS.V; }
35 bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }
36
37 Boolean operator-() const { return Boolean(V); }
38 Boolean operator-(const Boolean &Other) const { return Boolean(V - Other.V); }
39 Boolean operator~() const { return Boolean(true); }
40 Boolean operator!() const { return Boolean(!V); }
41
42 template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>>
43 explicit operator Ty() const {
44 return V;
45 }
46
47 APSInt toAPSInt() const {
48 return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
49 }
50 APSInt toAPSInt(unsigned NumBits) const {
51 return APSInt(toAPSInt().zextOrTrunc(width: NumBits), true);
52 }
53 APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); }
54
55 constexpr static unsigned bitWidth() { return 1; }
56 bool isZero() const { return !V; }
57 bool isMin() const { return isZero(); }
58
59 constexpr static bool isMinusOne() { return false; }
60 constexpr static bool isSigned() { return false; }
61 constexpr static bool isNegative() { return false; }
62 constexpr static bool isPositive() { return !isNegative(); }
63 constexpr static bool isNumber() { return true; }
64
65 ComparisonCategoryResult compare(const Boolean &RHS) const {
66 return Compare(X: V, Y: RHS.V);
67 }
68
69 unsigned countLeadingZeros() const { return V ? 0 : 1; }
70 unsigned getActiveBits() const { return V; }
71
72 Boolean truncate(unsigned TruncBits) const { return *this; }
73
74 static Boolean bitcastFromMemory(const std::byte *Buff, unsigned BitWidth) {
75 // Just load the first byte.
76 bool Val = static_cast<bool>(*Buff);
77 return Boolean(Val);
78 }
79
80 void bitcastToMemory(std::byte *Buff) const {
81 std::memcpy(dest: Buff, src: &V, n: sizeof(V));
82 }
83
84 void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
85 std::string toDiagnosticString(const ASTContext &Ctx) const {
86 std::string NameStr;
87 llvm::raw_string_ostream OS(NameStr);
88 print(OS);
89 return NameStr;
90 }
91
92 static Boolean min(unsigned NumBits) { return Boolean(false); }
93 static Boolean max(unsigned NumBits) { return Boolean(true); }
94
95 template <typename T> static Boolean from(T Value) {
96 if constexpr (std::is_integral<T>::value)
97 return Boolean(Value != 0);
98 return Boolean(static_cast<decltype(Boolean::V)>(Value) != 0);
99 }
100
101 template <unsigned SrcBits, bool SrcSign>
102 static std::enable_if_t<SrcBits != 0, Boolean>
103 from(Integral<SrcBits, SrcSign> Value) {
104 return Boolean(!Value.isZero());
105 }
106
107 static Boolean zero() { return from(Value: false); }
108
109 template <typename T> static Boolean from(T Value, unsigned NumBits) {
110 return Boolean(Value);
111 }
112
113 static bool increment(Boolean A, Boolean *R) {
114 *R = Boolean(true);
115 return false;
116 }
117
118 static bool decrement(Boolean A, Boolean *R) {
119 llvm_unreachable("Cannot decrement booleans");
120 }
121
122 static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
123 *R = Boolean(A.V || B.V);
124 return false;
125 }
126
127 static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
128 *R = Boolean(A.V ^ B.V);
129 return false;
130 }
131
132 static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
133 *R = Boolean(A.V && B.V);
134 return false;
135 }
136
137 static bool inv(Boolean A, Boolean *R) {
138 *R = Boolean(!A.V);
139 return false;
140 }
141
142 static bool neg(Boolean A, Boolean *R) {
143 *R = Boolean(A.V);
144 return false;
145 }
146};
147
148inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {
149 B.print(OS);
150 return OS;
151}
152
153} // namespace interp
154} // namespace clang
155
156#endif
157