annotate clang/lib/AST/Interp/Boolean.h @ 150:1d019706d866

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