Boolean.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 {
  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 unsigned() const { return V; }
  40. explicit operator int64_t() const { return V; }
  41. explicit operator uint64_t() const { return V; }
  42. APSInt toAPSInt() const {
  43. return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
  44. }
  45. APSInt toAPSInt(unsigned NumBits) const {
  46. return APSInt(toAPSInt().zextOrTrunc(NumBits), true);
  47. }
  48. APValue toAPValue() const { return APValue(toAPSInt()); }
  49. Boolean toUnsigned() const { return *this; }
  50. constexpr static unsigned bitWidth() { return true; }
  51. bool isZero() const { return !V; }
  52. bool isMin() const { return isZero(); }
  53. constexpr static bool isMinusOne() { return false; }
  54. constexpr static bool isSigned() { return false; }
  55. constexpr static bool isNegative() { return false; }
  56. constexpr static bool isPositive() { return !isNegative(); }
  57. ComparisonCategoryResult compare(const Boolean &RHS) const {
  58. return Compare(V, RHS.V);
  59. }
  60. unsigned countLeadingZeros() const { return V ? 0 : 1; }
  61. Boolean truncate(unsigned TruncBits) const { return *this; }
  62. void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
  63. static Boolean min(unsigned NumBits) { return Boolean(false); }
  64. static Boolean max(unsigned NumBits) { return Boolean(true); }
  65. template <typename T>
  66. static std::enable_if_t<std::is_integral<T>::value, Boolean> from(T Value) {
  67. return Boolean(Value != 0);
  68. }
  69. template <unsigned SrcBits, bool SrcSign>
  70. static std::enable_if_t<SrcBits != 0, Boolean>
  71. from(Integral<SrcBits, SrcSign> Value) {
  72. return Boolean(!Value.isZero());
  73. }
  74. template <bool SrcSign>
  75. static Boolean from(Integral<0, SrcSign> Value) {
  76. return Boolean(!Value.isZero());
  77. }
  78. static Boolean zero() { return from(false); }
  79. template <typename T>
  80. static Boolean from(T Value, unsigned NumBits) {
  81. return Boolean(Value);
  82. }
  83. static bool inRange(int64_t Value, unsigned NumBits) {
  84. return Value == 0 || Value == 1;
  85. }
  86. static bool increment(Boolean A, Boolean *R) {
  87. *R = Boolean(true);
  88. return false;
  89. }
  90. static bool decrement(Boolean A, Boolean *R) {
  91. llvm_unreachable("Cannot decrement booleans");
  92. }
  93. static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
  94. *R = Boolean(A.V || B.V);
  95. return false;
  96. }
  97. static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
  98. *R = Boolean(A.V ^ B.V);
  99. return false;
  100. }
  101. static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
  102. *R = Boolean(A.V && B.V);
  103. return false;
  104. }
  105. };
  106. inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {
  107. B.print(OS);
  108. return OS;
  109. }
  110. } // namespace interp
  111. } // namespace clang
  112. #endif