xref: /freebsd-src/contrib/llvm-project/clang/lib/AST/Interp/Boolean.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1a7dea167SDimitry Andric //===--- Boolean.h - Wrapper for boolean types for the VM -------*- C++ -*-===//
2a7dea167SDimitry Andric //
3a7dea167SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a7dea167SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5a7dea167SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a7dea167SDimitry Andric //
7a7dea167SDimitry Andric //===----------------------------------------------------------------------===//
8a7dea167SDimitry Andric 
9a7dea167SDimitry Andric #ifndef LLVM_CLANG_AST_INTERP_BOOLEAN_H
10a7dea167SDimitry Andric #define LLVM_CLANG_AST_INTERP_BOOLEAN_H
11a7dea167SDimitry Andric 
12a7dea167SDimitry Andric #include <cstddef>
13a7dea167SDimitry Andric #include <cstdint>
14a7dea167SDimitry Andric #include "Integral.h"
15a7dea167SDimitry Andric #include "clang/AST/APValue.h"
16a7dea167SDimitry Andric #include "clang/AST/ComparisonCategories.h"
17a7dea167SDimitry Andric #include "llvm/ADT/APSInt.h"
18a7dea167SDimitry Andric #include "llvm/Support/MathExtras.h"
19a7dea167SDimitry Andric #include "llvm/Support/raw_ostream.h"
20a7dea167SDimitry Andric 
21a7dea167SDimitry Andric namespace clang {
22a7dea167SDimitry Andric namespace interp {
23a7dea167SDimitry Andric 
24a7dea167SDimitry Andric /// Wrapper around boolean types.
25bdd1243dSDimitry Andric class Boolean final {
26a7dea167SDimitry Andric  private:
27a7dea167SDimitry Andric   /// Underlying boolean.
28a7dea167SDimitry Andric   bool V;
29a7dea167SDimitry Andric 
30a7dea167SDimitry Andric  public:
31a7dea167SDimitry Andric   /// Zero-initializes a boolean.
32a7dea167SDimitry Andric   Boolean() : V(false) {}
33*06c3fb27SDimitry Andric   explicit Boolean(bool V) : V(V) {}
34a7dea167SDimitry Andric 
35a7dea167SDimitry Andric   bool operator<(Boolean RHS) const { return V < RHS.V; }
36a7dea167SDimitry Andric   bool operator>(Boolean RHS) const { return V > RHS.V; }
37a7dea167SDimitry Andric   bool operator<=(Boolean RHS) const { return V <= RHS.V; }
38a7dea167SDimitry Andric   bool operator>=(Boolean RHS) const { return V >= RHS.V; }
39a7dea167SDimitry Andric   bool operator==(Boolean RHS) const { return V == RHS.V; }
40a7dea167SDimitry Andric   bool operator!=(Boolean RHS) const { return V != RHS.V; }
41a7dea167SDimitry Andric 
42a7dea167SDimitry Andric   bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }
43a7dea167SDimitry Andric 
44a7dea167SDimitry Andric   Boolean operator-() const { return Boolean(V); }
45a7dea167SDimitry Andric   Boolean operator~() const { return Boolean(true); }
46a7dea167SDimitry Andric 
47bdd1243dSDimitry Andric   explicit operator int8_t() const { return V; }
48bdd1243dSDimitry Andric   explicit operator uint8_t() const { return V; }
49bdd1243dSDimitry Andric   explicit operator int16_t() const { return V; }
50bdd1243dSDimitry Andric   explicit operator uint16_t() const { return V; }
51bdd1243dSDimitry Andric   explicit operator int32_t() const { return V; }
52bdd1243dSDimitry Andric   explicit operator uint32_t() const { return V; }
53a7dea167SDimitry Andric   explicit operator int64_t() const { return V; }
54a7dea167SDimitry Andric   explicit operator uint64_t() const { return V; }
55bdd1243dSDimitry Andric   explicit operator bool() const { return V; }
56a7dea167SDimitry Andric 
57a7dea167SDimitry Andric   APSInt toAPSInt() const {
58a7dea167SDimitry Andric     return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
59a7dea167SDimitry Andric   }
60a7dea167SDimitry Andric   APSInt toAPSInt(unsigned NumBits) const {
61a7dea167SDimitry Andric     return APSInt(toAPSInt().zextOrTrunc(NumBits), true);
62a7dea167SDimitry Andric   }
63a7dea167SDimitry Andric   APValue toAPValue() const { return APValue(toAPSInt()); }
64a7dea167SDimitry Andric 
65a7dea167SDimitry Andric   Boolean toUnsigned() const { return *this; }
66a7dea167SDimitry Andric 
67*06c3fb27SDimitry Andric   constexpr static unsigned bitWidth() { return 1; }
68a7dea167SDimitry Andric   bool isZero() const { return !V; }
69a7dea167SDimitry Andric   bool isMin() const { return isZero(); }
70a7dea167SDimitry Andric 
71a7dea167SDimitry Andric   constexpr static bool isMinusOne() { return false; }
72a7dea167SDimitry Andric 
73a7dea167SDimitry Andric   constexpr static bool isSigned() { return false; }
74a7dea167SDimitry Andric 
75a7dea167SDimitry Andric   constexpr static bool isNegative() { return false; }
76a7dea167SDimitry Andric   constexpr static bool isPositive() { return !isNegative(); }
77a7dea167SDimitry Andric 
78a7dea167SDimitry Andric   ComparisonCategoryResult compare(const Boolean &RHS) const {
79a7dea167SDimitry Andric     return Compare(V, RHS.V);
80a7dea167SDimitry Andric   }
81a7dea167SDimitry Andric 
82a7dea167SDimitry Andric   unsigned countLeadingZeros() const { return V ? 0 : 1; }
83a7dea167SDimitry Andric 
84a7dea167SDimitry Andric   Boolean truncate(unsigned TruncBits) const { return *this; }
85a7dea167SDimitry Andric 
86a7dea167SDimitry Andric   void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
87a7dea167SDimitry Andric 
88a7dea167SDimitry Andric   static Boolean min(unsigned NumBits) { return Boolean(false); }
89a7dea167SDimitry Andric   static Boolean max(unsigned NumBits) { return Boolean(true); }
90a7dea167SDimitry Andric 
91bdd1243dSDimitry Andric   template <typename T> static Boolean from(T Value) {
92bdd1243dSDimitry Andric     if constexpr (std::is_integral<T>::value)
93a7dea167SDimitry Andric       return Boolean(Value != 0);
94bdd1243dSDimitry Andric     return Boolean(static_cast<decltype(Boolean::V)>(Value) != 0);
95a7dea167SDimitry Andric   }
96a7dea167SDimitry Andric 
97a7dea167SDimitry Andric   template <unsigned SrcBits, bool SrcSign>
985ffd83dbSDimitry Andric   static std::enable_if_t<SrcBits != 0, Boolean>
995ffd83dbSDimitry Andric   from(Integral<SrcBits, SrcSign> Value) {
100a7dea167SDimitry Andric     return Boolean(!Value.isZero());
101a7dea167SDimitry Andric   }
102a7dea167SDimitry Andric 
103a7dea167SDimitry Andric   template <bool SrcSign>
104a7dea167SDimitry Andric   static Boolean from(Integral<0, SrcSign> Value) {
105a7dea167SDimitry Andric     return Boolean(!Value.isZero());
106a7dea167SDimitry Andric   }
107a7dea167SDimitry Andric 
108a7dea167SDimitry Andric   static Boolean zero() { return from(false); }
109a7dea167SDimitry Andric 
110a7dea167SDimitry Andric   template <typename T>
111a7dea167SDimitry Andric   static Boolean from(T Value, unsigned NumBits) {
112a7dea167SDimitry Andric     return Boolean(Value);
113a7dea167SDimitry Andric   }
114a7dea167SDimitry Andric 
115a7dea167SDimitry Andric   static bool inRange(int64_t Value, unsigned NumBits) {
116a7dea167SDimitry Andric     return Value == 0 || Value == 1;
117a7dea167SDimitry Andric   }
118a7dea167SDimitry Andric 
119a7dea167SDimitry Andric   static bool increment(Boolean A, Boolean *R) {
120a7dea167SDimitry Andric     *R = Boolean(true);
121a7dea167SDimitry Andric     return false;
122a7dea167SDimitry Andric   }
123a7dea167SDimitry Andric 
124a7dea167SDimitry Andric   static bool decrement(Boolean A, Boolean *R) {
125a7dea167SDimitry Andric     llvm_unreachable("Cannot decrement booleans");
126a7dea167SDimitry Andric   }
127a7dea167SDimitry Andric 
128a7dea167SDimitry Andric   static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
129a7dea167SDimitry Andric     *R = Boolean(A.V || B.V);
130a7dea167SDimitry Andric     return false;
131a7dea167SDimitry Andric   }
132a7dea167SDimitry Andric 
133a7dea167SDimitry Andric   static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
134a7dea167SDimitry Andric     *R = Boolean(A.V ^ B.V);
135a7dea167SDimitry Andric     return false;
136a7dea167SDimitry Andric   }
137a7dea167SDimitry Andric 
138a7dea167SDimitry Andric   static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
139a7dea167SDimitry Andric     *R = Boolean(A.V && B.V);
140a7dea167SDimitry Andric     return false;
141a7dea167SDimitry Andric   }
142bdd1243dSDimitry Andric 
143bdd1243dSDimitry Andric   static bool inv(Boolean A, Boolean *R) {
144bdd1243dSDimitry Andric     *R = Boolean(!A.V);
145bdd1243dSDimitry Andric     return false;
146bdd1243dSDimitry Andric   }
147bdd1243dSDimitry Andric 
148bdd1243dSDimitry Andric   static bool neg(Boolean A, Boolean *R) {
149bdd1243dSDimitry Andric     *R = Boolean(A.V);
150bdd1243dSDimitry Andric     return false;
151bdd1243dSDimitry Andric   }
152a7dea167SDimitry Andric };
153a7dea167SDimitry Andric 
154a7dea167SDimitry Andric inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {
155a7dea167SDimitry Andric   B.print(OS);
156a7dea167SDimitry Andric   return OS;
157a7dea167SDimitry Andric }
158a7dea167SDimitry Andric 
159a7dea167SDimitry Andric }  // namespace interp
160a7dea167SDimitry Andric }  // namespace clang
161a7dea167SDimitry Andric 
162a7dea167SDimitry Andric #endif
163