xref: /freebsd-src/contrib/llvm-project/clang/lib/AST/Interp/Boolean.h (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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.
25a7dea167SDimitry Andric class Boolean {
26a7dea167SDimitry Andric  private:
27a7dea167SDimitry Andric   /// Underlying boolean.
28a7dea167SDimitry Andric   bool V;
29a7dea167SDimitry Andric 
30a7dea167SDimitry Andric   /// Construct a wrapper from a boolean.
31a7dea167SDimitry Andric   explicit Boolean(bool V) : V(V) {}
32a7dea167SDimitry Andric 
33a7dea167SDimitry Andric  public:
34a7dea167SDimitry Andric   /// Zero-initializes a boolean.
35a7dea167SDimitry Andric   Boolean() : V(false) {}
36a7dea167SDimitry Andric 
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   bool operator==(Boolean RHS) const { return V == RHS.V; }
42a7dea167SDimitry Andric   bool operator!=(Boolean RHS) const { return V != RHS.V; }
43a7dea167SDimitry Andric 
44a7dea167SDimitry Andric   bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }
45a7dea167SDimitry Andric 
46a7dea167SDimitry Andric   Boolean operator-() const { return Boolean(V); }
47a7dea167SDimitry Andric   Boolean operator~() const { return Boolean(true); }
48a7dea167SDimitry Andric 
49a7dea167SDimitry Andric   explicit operator unsigned() const { return V; }
50a7dea167SDimitry Andric   explicit operator int64_t() const { return V; }
51a7dea167SDimitry Andric   explicit operator uint64_t() const { return V; }
52a7dea167SDimitry Andric 
53a7dea167SDimitry Andric   APSInt toAPSInt() const {
54a7dea167SDimitry Andric     return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
55a7dea167SDimitry Andric   }
56a7dea167SDimitry Andric   APSInt toAPSInt(unsigned NumBits) const {
57a7dea167SDimitry Andric     return APSInt(toAPSInt().zextOrTrunc(NumBits), true);
58a7dea167SDimitry Andric   }
59a7dea167SDimitry Andric   APValue toAPValue() const { return APValue(toAPSInt()); }
60a7dea167SDimitry Andric 
61a7dea167SDimitry Andric   Boolean toUnsigned() const { return *this; }
62a7dea167SDimitry Andric 
63a7dea167SDimitry Andric   constexpr static unsigned bitWidth() { return true; }
64a7dea167SDimitry Andric   bool isZero() const { return !V; }
65a7dea167SDimitry Andric   bool isMin() const { return isZero(); }
66a7dea167SDimitry Andric 
67a7dea167SDimitry Andric   constexpr static bool isMinusOne() { return false; }
68a7dea167SDimitry Andric 
69a7dea167SDimitry Andric   constexpr static bool isSigned() { return false; }
70a7dea167SDimitry Andric 
71a7dea167SDimitry Andric   constexpr static bool isNegative() { return false; }
72a7dea167SDimitry Andric   constexpr static bool isPositive() { return !isNegative(); }
73a7dea167SDimitry Andric 
74a7dea167SDimitry Andric   ComparisonCategoryResult compare(const Boolean &RHS) const {
75a7dea167SDimitry Andric     return Compare(V, RHS.V);
76a7dea167SDimitry Andric   }
77a7dea167SDimitry Andric 
78a7dea167SDimitry Andric   unsigned countLeadingZeros() const { return V ? 0 : 1; }
79a7dea167SDimitry Andric 
80a7dea167SDimitry Andric   Boolean truncate(unsigned TruncBits) const { return *this; }
81a7dea167SDimitry Andric 
82a7dea167SDimitry Andric   void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
83a7dea167SDimitry Andric 
84a7dea167SDimitry Andric   static Boolean min(unsigned NumBits) { return Boolean(false); }
85a7dea167SDimitry Andric   static Boolean max(unsigned NumBits) { return Boolean(true); }
86a7dea167SDimitry Andric 
87a7dea167SDimitry Andric   template <typename T>
88*5ffd83dbSDimitry Andric   static std::enable_if_t<std::is_integral<T>::value, Boolean> from(T Value) {
89a7dea167SDimitry Andric     return Boolean(Value != 0);
90a7dea167SDimitry Andric   }
91a7dea167SDimitry Andric 
92a7dea167SDimitry Andric   template <unsigned SrcBits, bool SrcSign>
93*5ffd83dbSDimitry Andric   static std::enable_if_t<SrcBits != 0, Boolean>
94*5ffd83dbSDimitry Andric   from(Integral<SrcBits, SrcSign> Value) {
95a7dea167SDimitry Andric     return Boolean(!Value.isZero());
96a7dea167SDimitry Andric   }
97a7dea167SDimitry Andric 
98a7dea167SDimitry Andric   template <bool SrcSign>
99a7dea167SDimitry Andric   static Boolean from(Integral<0, SrcSign> Value) {
100a7dea167SDimitry Andric     return Boolean(!Value.isZero());
101a7dea167SDimitry Andric   }
102a7dea167SDimitry Andric 
103a7dea167SDimitry Andric   static Boolean zero() { return from(false); }
104a7dea167SDimitry Andric 
105a7dea167SDimitry Andric   template <typename T>
106a7dea167SDimitry Andric   static Boolean from(T Value, unsigned NumBits) {
107a7dea167SDimitry Andric     return Boolean(Value);
108a7dea167SDimitry Andric   }
109a7dea167SDimitry Andric 
110a7dea167SDimitry Andric   static bool inRange(int64_t Value, unsigned NumBits) {
111a7dea167SDimitry Andric     return Value == 0 || Value == 1;
112a7dea167SDimitry Andric   }
113a7dea167SDimitry Andric 
114a7dea167SDimitry Andric   static bool increment(Boolean A, Boolean *R) {
115a7dea167SDimitry Andric     *R = Boolean(true);
116a7dea167SDimitry Andric     return false;
117a7dea167SDimitry Andric   }
118a7dea167SDimitry Andric 
119a7dea167SDimitry Andric   static bool decrement(Boolean A, Boolean *R) {
120a7dea167SDimitry Andric     llvm_unreachable("Cannot decrement booleans");
121a7dea167SDimitry Andric   }
122a7dea167SDimitry Andric 
123a7dea167SDimitry Andric   static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
124a7dea167SDimitry Andric     *R = Boolean(A.V || B.V);
125a7dea167SDimitry Andric     return false;
126a7dea167SDimitry Andric   }
127a7dea167SDimitry Andric 
128a7dea167SDimitry Andric   static bool sub(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 mul(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 
139a7dea167SDimitry Andric inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {
140a7dea167SDimitry Andric   B.print(OS);
141a7dea167SDimitry Andric   return OS;
142a7dea167SDimitry Andric }
143a7dea167SDimitry Andric 
144a7dea167SDimitry Andric }  // namespace interp
145a7dea167SDimitry Andric }  // namespace clang
146a7dea167SDimitry Andric 
147a7dea167SDimitry Andric #endif
148