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