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