clang 17.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 { return Boolean(true); }
46
47 explicit operator int8_t() const { return V; }
48 explicit operator uint8_t() const { return V; }
49 explicit operator int16_t() const { return V; }
50 explicit operator uint16_t() const { return V; }
51 explicit operator int32_t() const { return V; }
52 explicit operator uint32_t() const { return V; }
53 explicit operator int64_t() const { return V; }
54 explicit operator uint64_t() const { return V; }
55 explicit operator bool() const { return V; }
56
57 APSInt toAPSInt() const {
58 return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
59 }
60 APSInt toAPSInt(unsigned NumBits) const {
61 return APSInt(toAPSInt().zextOrTrunc(NumBits), true);
62 }
63 APValue toAPValue() const { return APValue(toAPSInt()); }
64
65 Boolean toUnsigned() const { return *this; }
66
67 constexpr static unsigned bitWidth() { return true; }
68 bool isZero() const { return !V; }
69 bool isMin() const { return isZero(); }
70
71 constexpr static bool isMinusOne() { return false; }
72
73 constexpr static bool isSigned() { return false; }
74
75 constexpr static bool isNegative() { return false; }
76 constexpr static bool isPositive() { return !isNegative(); }
77
79 return Compare(V, RHS.V);
80 }
81
82 unsigned countLeadingZeros() const { return V ? 0 : 1; }
83
84 Boolean truncate(unsigned TruncBits) const { return *this; }
85
86 void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
87
88 static Boolean min(unsigned NumBits) { return Boolean(false); }
89 static Boolean max(unsigned NumBits) { return Boolean(true); }
90
91 template <typename T> static Boolean from(T Value) {
92 if constexpr (std::is_integral<T>::value)
93 return Boolean(Value != 0);
94 return Boolean(static_cast<decltype(Boolean::V)>(Value) != 0);
95 }
96
97 template <unsigned SrcBits, bool SrcSign>
98 static std::enable_if_t<SrcBits != 0, Boolean>
100 return Boolean(!Value.isZero());
101 }
102
103 template <bool SrcSign>
105 return Boolean(!Value.isZero());
106 }
107
108 static Boolean zero() { return from(false); }
109
110 template <typename T>
111 static Boolean from(T Value, unsigned NumBits) {
112 return Boolean(Value);
113 }
114
115 static bool inRange(int64_t Value, unsigned NumBits) {
116 return Value == 0 || Value == 1;
117 }
118
119 static bool increment(Boolean A, Boolean *R) {
120 *R = Boolean(true);
121 return false;
122 }
123
124 static bool decrement(Boolean A, Boolean *R) {
125 llvm_unreachable("Cannot decrement booleans");
126 }
127
128 static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
129 *R = Boolean(A.V || B.V);
130 return false;
131 }
132
133 static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
134 *R = Boolean(A.V ^ B.V);
135 return false;
136 }
137
138 static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
139 *R = Boolean(A.V && B.V);
140 return false;
141 }
142
143 static bool inv(Boolean A, Boolean *R) {
144 *R = Boolean(!A.V);
145 return false;
146 }
147
148 static bool neg(Boolean A, Boolean *R) {
149 *R = Boolean(A.V);
150 return false;
151 }
152};
153
154inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {
155 B.print(OS);
156 return OS;
157}
158
159} // namespace interp
160} // namespace clang
161
162#endif
#define V(N, I)
Definition: ASTContext.h:3230
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
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:111
ComparisonCategoryResult compare(const Boolean &RHS) const
Definition: Boolean.h:78
static Boolean from(Integral< 0, SrcSign > Value)
Definition: Boolean.h:104
static Boolean max(unsigned NumBits)
Definition: Boolean.h:89
bool operator!=(Boolean RHS) const
Definition: Boolean.h:40
static Boolean min(unsigned NumBits)
Definition: Boolean.h:88
static bool inv(Boolean A, Boolean *R)
Definition: Boolean.h:143
static bool decrement(Boolean A, Boolean *R)
Definition: Boolean.h:124
APSInt toAPSInt() const
Definition: Boolean.h:57
bool operator>(unsigned RHS) const
Definition: Boolean.h:42
APSInt toAPSInt(unsigned NumBits) const
Definition: Boolean.h:60
Boolean toUnsigned() const
Definition: Boolean.h:65
static constexpr unsigned bitWidth()
Definition: Boolean.h:67
static constexpr bool isSigned()
Definition: Boolean.h:73
bool operator<(Boolean RHS) const
Definition: Boolean.h:35
static Boolean zero()
Definition: Boolean.h:108
bool operator==(Boolean RHS) const
Definition: Boolean.h:39
Boolean truncate(unsigned TruncBits) const
Definition: Boolean.h:84
bool isZero() const
Definition: Boolean.h:68
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:99
APValue toAPValue() const
Definition: Boolean.h:63
static constexpr bool isNegative()
Definition: Boolean.h:75
static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R)
Definition: Boolean.h:138
unsigned countLeadingZeros() const
Definition: Boolean.h:82
void print(llvm::raw_ostream &OS) const
Definition: Boolean.h:86
static constexpr bool isPositive()
Definition: Boolean.h:76
Boolean()
Zero-initializes a boolean.
Definition: Boolean.h:32
static bool neg(Boolean A, Boolean *R)
Definition: Boolean.h:148
static Boolean from(T Value)
Definition: Boolean.h:91
static bool inRange(int64_t Value, unsigned NumBits)
Definition: Boolean.h:115
bool isMin() const
Definition: Boolean.h:69
bool operator>=(Boolean RHS) const
Definition: Boolean.h:38
static bool increment(Boolean A, Boolean *R)
Definition: Boolean.h:119
static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R)
Definition: Boolean.h:133
static constexpr bool isMinusOne()
Definition: Boolean.h:71
static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R)
Definition: Boolean.h:128
Boolean operator~() const
Definition: Boolean.h:45
Wrapper around numeric types.
Definition: Integral.h:48
llvm::APInt APInt
Definition: Integral.h:29
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:154
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
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
#define false
Definition: stdbool.h:22
#define bool
Definition: stdbool.h:20