clang 20.0.0git
Boolean.h
Go to the documentation of this file.
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"
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
21namespace clang {
22namespace interp {
23
24/// Wrapper around boolean types.
25class Boolean final {
26private:
27 /// Underlying boolean.
28 bool V;
29
30public:
31 /// Zero-initializes a boolean.
32 Boolean() : V(false) {}
33 Boolean(const llvm::APSInt &I) : V(!I.isZero()) {}
34 explicit Boolean(bool V) : V(V) {}
35
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 bool operator!=(Boolean RHS) const { return V != RHS.V; }
42
43 bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }
44
45 Boolean operator-() const { return Boolean(V); }
46 Boolean operator-(const Boolean &Other) const { return Boolean(V - Other.V); }
47 Boolean operator~() const { return Boolean(true); }
48 Boolean operator!() const { return Boolean(!V); }
49
50 template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>>
51 explicit operator Ty() const {
52 return V;
53 }
54
55 APSInt toAPSInt() const {
56 return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
57 }
58 APSInt toAPSInt(unsigned NumBits) const {
59 return APSInt(toAPSInt().zextOrTrunc(NumBits), true);
60 }
61 APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); }
62
63 Boolean toUnsigned() const { return *this; }
64
65 constexpr static unsigned bitWidth() { return 1; }
66 bool isZero() const { return !V; }
67 bool isMin() const { return isZero(); }
68
69 constexpr static bool isMinusOne() { return false; }
70
71 constexpr static bool isSigned() { return false; }
72
73 constexpr static bool isNegative() { return false; }
74 constexpr static bool isPositive() { return !isNegative(); }
75
77 return Compare(V, RHS.V);
78 }
79
80 unsigned countLeadingZeros() const { return V ? 0 : 1; }
81
82 Boolean truncate(unsigned TruncBits) const { return *this; }
83
84 static Boolean bitcastFromMemory(const std::byte *Buff, unsigned BitWidth) {
85 // Just load the first byte.
86 bool Val = static_cast<bool>(*Buff);
87 return Boolean(Val);
88 }
89
90 void bitcastToMemory(std::byte *Buff) { std::memcpy(Buff, &V, sizeof(V)); }
91
92 void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
93 std::string toDiagnosticString(const ASTContext &Ctx) const {
94 std::string NameStr;
95 llvm::raw_string_ostream OS(NameStr);
96 print(OS);
97 return NameStr;
98 }
99
100 static Boolean min(unsigned NumBits) { return Boolean(false); }
101 static Boolean max(unsigned NumBits) { return Boolean(true); }
102
103 template <typename T> static Boolean from(T Value) {
104 if constexpr (std::is_integral<T>::value)
105 return Boolean(Value != 0);
106 return Boolean(static_cast<decltype(Boolean::V)>(Value) != 0);
107 }
108
109 template <unsigned SrcBits, bool SrcSign>
110 static std::enable_if_t<SrcBits != 0, Boolean>
112 return Boolean(!Value.isZero());
113 }
114
115 static Boolean zero() { return from(false); }
116
117 template <typename T> static Boolean from(T Value, unsigned NumBits) {
118 return Boolean(Value);
119 }
120
121 static bool inRange(int64_t Value, unsigned NumBits) {
122 return Value == 0 || Value == 1;
123 }
124
125 static bool increment(Boolean A, Boolean *R) {
126 *R = Boolean(true);
127 return false;
128 }
129
130 static bool decrement(Boolean A, Boolean *R) {
131 llvm_unreachable("Cannot decrement booleans");
132 }
133
134 static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
135 *R = Boolean(A.V || B.V);
136 return false;
137 }
138
139 static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
140 *R = Boolean(A.V ^ B.V);
141 return false;
142 }
143
144 static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
145 *R = Boolean(A.V && B.V);
146 return false;
147 }
148
149 static bool inv(Boolean A, Boolean *R) {
150 *R = Boolean(!A.V);
151 return false;
152 }
153
154 static bool neg(Boolean A, Boolean *R) {
155 *R = Boolean(A.V);
156 return false;
157 }
158};
159
160inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {
161 B.print(OS);
162 return OS;
163}
164
165} // namespace interp
166} // namespace clang
167
168#endif
#define V(N, I)
Definition: ASTContext.h:3443
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
Wrapper around boolean types.
Definition: Boolean.h:25
Boolean(const llvm::APSInt &I)
Definition: Boolean.h:33
Boolean operator-() const
Definition: Boolean.h:45
static Boolean from(T Value, unsigned NumBits)
Definition: Boolean.h:117
ComparisonCategoryResult compare(const Boolean &RHS) const
Definition: Boolean.h:76
static Boolean max(unsigned NumBits)
Definition: Boolean.h:101
bool operator!=(Boolean RHS) const
Definition: Boolean.h:41
static Boolean min(unsigned NumBits)
Definition: Boolean.h:100
Boolean operator-(const Boolean &Other) const
Definition: Boolean.h:46
static bool inv(Boolean A, Boolean *R)
Definition: Boolean.h:149
static bool decrement(Boolean A, Boolean *R)
Definition: Boolean.h:130
APSInt toAPSInt() const
Definition: Boolean.h:55
bool operator>(unsigned RHS) const
Definition: Boolean.h:43
APSInt toAPSInt(unsigned NumBits) const
Definition: Boolean.h:58
Boolean toUnsigned() const
Definition: Boolean.h:63
static constexpr unsigned bitWidth()
Definition: Boolean.h:65
static constexpr bool isSigned()
Definition: Boolean.h:71
std::string toDiagnosticString(const ASTContext &Ctx) const
Definition: Boolean.h:93
bool operator<(Boolean RHS) const
Definition: Boolean.h:36
APValue toAPValue(const ASTContext &) const
Definition: Boolean.h:61
static Boolean zero()
Definition: Boolean.h:115
bool operator==(Boolean RHS) const
Definition: Boolean.h:40
Boolean truncate(unsigned TruncBits) const
Definition: Boolean.h:82
bool isZero() const
Definition: Boolean.h:66
bool operator>(Boolean RHS) const
Definition: Boolean.h:37
bool operator<=(Boolean RHS) const
Definition: Boolean.h:38
static std::enable_if_t< SrcBits !=0, Boolean > from(Integral< SrcBits, SrcSign > Value)
Definition: Boolean.h:111
Boolean operator!() const
Definition: Boolean.h:48
static constexpr bool isNegative()
Definition: Boolean.h:73
static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R)
Definition: Boolean.h:144
unsigned countLeadingZeros() const
Definition: Boolean.h:80
void print(llvm::raw_ostream &OS) const
Definition: Boolean.h:92
static constexpr bool isPositive()
Definition: Boolean.h:74
Boolean()
Zero-initializes a boolean.
Definition: Boolean.h:32
static bool neg(Boolean A, Boolean *R)
Definition: Boolean.h:154
static Boolean from(T Value)
Definition: Boolean.h:103
static bool inRange(int64_t Value, unsigned NumBits)
Definition: Boolean.h:121
bool isMin() const
Definition: Boolean.h:67
bool operator>=(Boolean RHS) const
Definition: Boolean.h:39
static bool increment(Boolean A, Boolean *R)
Definition: Boolean.h:125
static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R)
Definition: Boolean.h:139
static constexpr bool isMinusOne()
Definition: Boolean.h:69
static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R)
Definition: Boolean.h:134
static Boolean bitcastFromMemory(const std::byte *Buff, unsigned BitWidth)
Definition: Boolean.h:84
void bitcastToMemory(std::byte *Buff)
Definition: Boolean.h:90
Boolean operator~() const
Definition: Boolean.h:47
Wrapper around numeric types.
Definition: Integral.h:66
llvm::APInt APInt
Definition: FixedPoint.h:19
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:160
ComparisonCategoryResult Compare(const T &X, const T &Y)
Helper to compare two comparable types.
Definition: Primitives.h:25
llvm::APSInt APSInt
Definition: FixedPoint.h:20
The JSON file list parser is used to communicate input to InstallAPI.
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
const FunctionProtoType * T
@ Other
Other implicit parameter.
#define false
Definition: stdbool.h:26