clang 19.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 <cstddef>
13#include <cstdint>
14#include "Integral.h"
15#include "clang/AST/APValue.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/Support/MathExtras.h"
19#include "llvm/Support/raw_ostream.h"
20
21namespace clang {
22namespace interp {
23
24/// Wrapper around boolean types.
25class Boolean final {
26 private:
27 /// Underlying boolean.
28 bool V;
29
30 public:
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<=(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
42 bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }
43
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); }
47
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; }
57
58 APSInt toAPSInt() const {
59 return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
60 }
61 APSInt toAPSInt(unsigned NumBits) const {
62 return APSInt(toAPSInt().zextOrTrunc(NumBits), true);
63 }
64 APValue toAPValue() const { return APValue(toAPSInt()); }
65
66 Boolean toUnsigned() const { return *this; }
67
68 constexpr static unsigned bitWidth() { return 1; }
69 bool isZero() const { return !V; }
70 bool isMin() const { return isZero(); }
71
72 constexpr static bool isMinusOne() { return false; }
73
74 constexpr static bool isSigned() { return false; }
75
76 constexpr static bool isNegative() { return false; }
77 constexpr static bool isPositive() { return !isNegative(); }
78
80 return Compare(V, RHS.V);
81 }
82
83 unsigned countLeadingZeros() const { return V ? 0 : 1; }
84
85 Boolean truncate(unsigned TruncBits) const { return *this; }
86
87 void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
88 std::string toDiagnosticString(const ASTContext &Ctx) const {
89 std::string NameStr;
90 llvm::raw_string_ostream OS(NameStr);
91 print(OS);
92 return NameStr;
93 }
94
95 static Boolean min(unsigned NumBits) { return Boolean(false); }
96 static Boolean max(unsigned NumBits) { return Boolean(true); }
97
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);
102 }
103
104 template <unsigned SrcBits, bool SrcSign>
105 static std::enable_if_t<SrcBits != 0, Boolean>
107 return Boolean(!Value.isZero());
108 }
109
110 static Boolean zero() { return from(false); }
111
112 template <typename T>
113 static Boolean from(T Value, unsigned NumBits) {
114 return Boolean(Value);
115 }
116
117 static bool inRange(int64_t Value, unsigned NumBits) {
118 return Value == 0 || Value == 1;
119 }
120
121 static bool increment(Boolean A, Boolean *R) {
122 *R = Boolean(true);
123 return false;
124 }
125
126 static bool decrement(Boolean A, Boolean *R) {
127 llvm_unreachable("Cannot decrement booleans");
128 }
129
130 static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
131 *R = Boolean(A.V || B.V);
132 return false;
133 }
134
135 static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
136 *R = Boolean(A.V ^ B.V);
137 return false;
138 }
139
140 static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
141 *R = Boolean(A.V && B.V);
142 return false;
143 }
144
145 static bool inv(Boolean A, Boolean *R) {
146 *R = Boolean(!A.V);
147 return false;
148 }
149
150 static bool neg(Boolean A, Boolean *R) {
151 *R = Boolean(A.V);
152 return false;
153 }
154};
155
156inline 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
#define V(N, I)
Definition: ASTContext.h:3273
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:182
Wrapper around boolean types.
Definition: Boolean.h:25
Boolean operator-() const
Definition: Boolean.h:44
static Boolean from(T Value, unsigned NumBits)
Definition: Boolean.h:113
ComparisonCategoryResult compare(const Boolean &RHS) const
Definition: Boolean.h:79
static Boolean max(unsigned NumBits)
Definition: Boolean.h:96
bool operator!=(Boolean RHS) const
Definition: Boolean.h:40
static Boolean min(unsigned NumBits)
Definition: Boolean.h:95
Boolean operator-(const Boolean &Other) const
Definition: Boolean.h:45
static bool inv(Boolean A, Boolean *R)
Definition: Boolean.h:145
static bool decrement(Boolean A, Boolean *R)
Definition: Boolean.h:126
APSInt toAPSInt() const
Definition: Boolean.h:58
bool operator>(unsigned RHS) const
Definition: Boolean.h:42
APSInt toAPSInt(unsigned NumBits) const
Definition: Boolean.h:61
Boolean toUnsigned() const
Definition: Boolean.h:66
static constexpr unsigned bitWidth()
Definition: Boolean.h:68
static constexpr bool isSigned()
Definition: Boolean.h:74
std::string toDiagnosticString(const ASTContext &Ctx) const
Definition: Boolean.h:88
bool operator<(Boolean RHS) const
Definition: Boolean.h:35
static Boolean zero()
Definition: Boolean.h:110
bool operator==(Boolean RHS) const
Definition: Boolean.h:39
Boolean truncate(unsigned TruncBits) const
Definition: Boolean.h:85
bool isZero() const
Definition: Boolean.h:69
bool operator>(Boolean RHS) const
Definition: Boolean.h:36
bool operator<=(Boolean RHS) const
Definition: Boolean.h:37
static std::enable_if_t< SrcBits !=0, Boolean > from(Integral< SrcBits, SrcSign > Value)
Definition: Boolean.h:106
APValue toAPValue() const
Definition: Boolean.h:64
static constexpr bool isNegative()
Definition: Boolean.h:76
static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R)
Definition: Boolean.h:140
unsigned countLeadingZeros() const
Definition: Boolean.h:83
void print(llvm::raw_ostream &OS) const
Definition: Boolean.h:87
static constexpr bool isPositive()
Definition: Boolean.h:77
Boolean()
Zero-initializes a boolean.
Definition: Boolean.h:32
static bool neg(Boolean A, Boolean *R)
Definition: Boolean.h:150
static Boolean from(T Value)
Definition: Boolean.h:98
static bool inRange(int64_t Value, unsigned NumBits)
Definition: Boolean.h:117
bool isMin() const
Definition: Boolean.h:70
bool operator>=(Boolean RHS) const
Definition: Boolean.h:38
static bool increment(Boolean A, Boolean *R)
Definition: Boolean.h:121
static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R)
Definition: Boolean.h:135
static constexpr bool isMinusOne()
Definition: Boolean.h:72
static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R)
Definition: Boolean.h:130
Boolean operator~() const
Definition: Boolean.h:46
Wrapper around numeric types.
Definition: Integral.h:50
llvm::APInt APInt
Definition: Integral.h:29
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:156
ComparisonCategoryResult Compare(const T &X, const T &Y)
Helper to compare two comparable types.
Definition: Primitives.h:25
llvm::APSInt APSInt
Definition: Floating.h:24
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:22
#define bool
Definition: stdbool.h:20