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