Boolean.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef LLVM_CLANG_AST_INTERP_BOOLEAN_H
  9. #define LLVM_CLANG_AST_INTERP_BOOLEAN_H
  10. #include <cstddef>
  11. #include <cstdint>
  12. #include "Integral.h"
  13. #include "clang/AST/APValue.h"
  14. #include "clang/AST/ComparisonCategories.h"
  15. #include "llvm/ADT/APSInt.h"
  16. #include "llvm/Support/MathExtras.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. namespace clang {
  19. namespace interp {
  20. /// Wrapper around boolean types.
  21. class Boolean final {
  22. private:
  23. /// Underlying boolean.
  24. bool V;
  25. /// Construct a wrapper from a boolean.
  26. explicit Boolean(bool V) : V(V) {}
  27. public:
  28. /// Zero-initializes a boolean.
  29. Boolean() : V(false) {}
  30. bool operator<(Boolean RHS) const { return V < RHS.V; }
  31. bool operator>(Boolean RHS) const { return V > RHS.V; }
  32. bool operator<=(Boolean RHS) const { return V <= RHS.V; }
  33. bool operator>=(Boolean RHS) const { return V >= RHS.V; }
  34. bool operator==(Boolean RHS) const { return V == RHS.V; }
  35. bool operator!=(Boolean RHS) const { return V != RHS.V; }
  36. bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }
  37. Boolean operator-() const { return Boolean(V); }
  38. Boolean operator~() const { return Boolean(true); }
  39. explicit operator int8_t() const { return V; }
  40. explicit operator uint8_t() const { return V; }
  41. explicit operator int16_t() const { return V; }
  42. explicit operator uint16_t() const { return V; }
  43. explicit operator int32_t() const { return V; }
  44. explicit operator uint32_t() const { return V; }
  45. explicit operator int64_t() const { return V; }
  46. explicit operator uint64_t() const { return V; }
  47. explicit operator bool() const { return V; }
  48. APSInt toAPSInt() const {
  49. return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
  50. }
  51. APSInt toAPSInt(unsigned NumBits) const {
  52. return APSInt(toAPSInt().zextOrTrunc(NumBits), true);
  53. }
  54. APValue toAPValue() const { return APValue(toAPSInt()); }
  55. Boolean toUnsigned() const { return *this; }
  56. constexpr static unsigned bitWidth() { return true; }
  57. bool isZero() const { return !V; }
  58. bool isMin() const { return isZero(); }
  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. ComparisonCategoryResult compare(const Boolean &RHS) const {
  64. return Compare(V, RHS.V);
  65. }
  66. unsigned countLeadingZeros() const { return V ? 0 : 1; }
  67. Boolean truncate(unsigned TruncBits) const { return *this; }
  68. void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
  69. static Boolean min(unsigned NumBits) { return Boolean(false); }
  70. static Boolean max(unsigned NumBits) { return Boolean(true); }
  71. template <typename T> static Boolean from(T Value) {
  72. if constexpr (std::is_integral<T>::value)
  73. return Boolean(Value != 0);
  74. return Boolean(static_cast<decltype(Boolean::V)>(Value) != 0);
  75. }
  76. template <unsigned SrcBits, bool SrcSign>
  77. static std::enable_if_t<SrcBits != 0, Boolean>
  78. from(Integral<SrcBits, SrcSign> Value) {
  79. return Boolean(!Value.isZero());
  80. }
  81. template <bool SrcSign>
  82. static Boolean from(Integral<0, SrcSign> Value) {
  83. return Boolean(!Value.isZero());
  84. }
  85. static Boolean zero() { return from(false); }
  86. template <typename T>
  87. static Boolean from(T Value, unsigned NumBits) {
  88. return Boolean(Value);
  89. }
  90. static bool inRange(int64_t Value, unsigned NumBits) {
  91. return Value == 0 || Value == 1;
  92. }
  93. static bool increment(Boolean A, Boolean *R) {
  94. *R = Boolean(true);
  95. return false;
  96. }
  97. static bool decrement(Boolean A, Boolean *R) {
  98. llvm_unreachable("Cannot decrement booleans");
  99. }
  100. static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
  101. *R = Boolean(A.V || B.V);
  102. return false;
  103. }
  104. static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
  105. *R = Boolean(A.V ^ B.V);
  106. return false;
  107. }
  108. static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
  109. *R = Boolean(A.V && B.V);
  110. return false;
  111. }
  112. static bool inv(Boolean A, Boolean *R) {
  113. *R = Boolean(!A.V);
  114. return false;
  115. }
  116. static bool neg(Boolean A, Boolean *R) {
  117. *R = Boolean(A.V);
  118. return false;
  119. }
  120. };
  121. inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {
  122. B.print(OS);
  123. return OS;
  124. }
  125. } // namespace interp
  126. } // namespace clang
  127. #endif