Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/AST/Interp/Boolean.h
Line
Count
Source (jump to first uncovered line)
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
9
#ifndef LLVM_CLANG_AST_INTERP_BOOLEAN_H
10
#define LLVM_CLANG_AST_INTERP_BOOLEAN_H
11
12
#include <cstddef>
13
#include <cstdint>
14
#include "Integral.h"
15
#include "clang/AST/APValue.h"
16
#include "clang/AST/ComparisonCategories.h"
17
#include "llvm/ADT/APSInt.h"
18
#include "llvm/Support/MathExtras.h"
19
#include "llvm/Support/raw_ostream.h"
20
21
namespace clang {
22
namespace interp {
23
24
/// Wrapper around boolean types.
25
class Boolean final {
26
 private:
27
  /// Underlying boolean.
28
  bool V;
29
30
 public:
31
  /// Zero-initializes a boolean.
32
0
  Boolean() : V(false) {}
33
0
  explicit Boolean(bool V) : V(V) {}
34
35
0
  bool operator<(Boolean RHS) const { return V < RHS.V; }
36
0
  bool operator>(Boolean RHS) const { return V > RHS.V; }
37
0
  bool operator<=(Boolean RHS) const { return V <= RHS.V; }
38
0
  bool operator>=(Boolean RHS) const { return V >= RHS.V; }
39
0
  bool operator==(Boolean RHS) const { return V == RHS.V; }
40
0
  bool operator!=(Boolean RHS) const { return V != RHS.V; }
41
42
0
  bool operator>(unsigned RHS) const { return static_cast<unsigned>(V) > RHS; }
43
44
0
  Boolean operator-() const { return Boolean(V); }
45
0
  Boolean operator-(const Boolean &Other) const { return Boolean(V - Other.V); }
46
0
  Boolean operator~() const { return Boolean(true); }
47
48
0
  explicit operator int8_t() const { return V; }
49
0
  explicit operator uint8_t() const { return V; }
50
0
  explicit operator int16_t() const { return V; }
51
0
  explicit operator uint16_t() const { return V; }
52
0
  explicit operator int32_t() const { return V; }
53
0
  explicit operator uint32_t() const { return V; }
54
0
  explicit operator int64_t() const { return V; }
55
0
  explicit operator uint64_t() const { return V; }
56
0
  explicit operator bool() const { return V; }
57
58
0
  APSInt toAPSInt() const {
59
0
    return APSInt(APInt(1, static_cast<uint64_t>(V), false), true);
60
0
  }
61
0
  APSInt toAPSInt(unsigned NumBits) const {
62
0
    return APSInt(toAPSInt().zextOrTrunc(NumBits), true);
63
0
  }
64
0
  APValue toAPValue() const { return APValue(toAPSInt()); }
65
66
0
  Boolean toUnsigned() const { return *this; }
67
68
0
  constexpr static unsigned bitWidth() { return 1; }
69
0
  bool isZero() const { return !V; }
70
0
  bool isMin() const { return isZero(); }
71
72
0
  constexpr static bool isMinusOne() { return false; }
73
74
0
  constexpr static bool isSigned() { return false; }
75
76
0
  constexpr static bool isNegative() { return false; }
77
0
  constexpr static bool isPositive() { return !isNegative(); }
78
79
0
  ComparisonCategoryResult compare(const Boolean &RHS) const {
80
0
    return Compare(V, RHS.V);
81
0
  }
82
83
0
  unsigned countLeadingZeros() const { return V ? 0 : 1; }
84
85
0
  Boolean truncate(unsigned TruncBits) const { return *this; }
86
87
0
  void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
88
0
  std::string toDiagnosticString(const ASTContext &Ctx) const {
89
0
    std::string NameStr;
90
0
    llvm::raw_string_ostream OS(NameStr);
91
0
    print(OS);
92
0
    return NameStr;
93
0
  }
94
95
0
  static Boolean min(unsigned NumBits) { return Boolean(false); }
96
0
  static Boolean max(unsigned NumBits) { return Boolean(true); }
97
98
0
  template <typename T> static Boolean from(T Value) {
99
0
    if constexpr (std::is_integral<T>::value)
100
0
      return Boolean(Value != 0);
101
0
    return Boolean(static_cast<decltype(Boolean::V)>(Value) != 0);
102
0
  }
Unexecuted instantiation: clang::interp::Boolean clang::interp::Boolean::from<bool>(bool)
Unexecuted instantiation: clang::interp::Boolean clang::interp::Boolean::from<clang::interp::Boolean>(clang::interp::Boolean)
Unexecuted instantiation: clang::interp::Boolean clang::interp::Boolean::from<clang::interp::IntegralAP<false> >(clang::interp::IntegralAP<false>)
Unexecuted instantiation: clang::interp::Boolean clang::interp::Boolean::from<clang::interp::IntegralAP<true> >(clang::interp::IntegralAP<true>)
Unexecuted instantiation: clang::interp::Boolean clang::interp::Boolean::from<unsigned int>(unsigned int)
Unexecuted instantiation: clang::interp::Boolean clang::interp::Boolean::from<long>(long)
103
104
  template <unsigned SrcBits, bool SrcSign>
105
  static std::enable_if_t<SrcBits != 0, Boolean>
106
0
  from(Integral<SrcBits, SrcSign> Value) {
107
0
    return Boolean(!Value.isZero());
108
0
  }
Unexecuted instantiation: std::__1::enable_if<(8u)!=(0), clang::interp::Boolean>::type clang::interp::Boolean::from<8u, false>(clang::interp::Integral<8u, false>)
Unexecuted instantiation: std::__1::enable_if<(8u)!=(0), clang::interp::Boolean>::type clang::interp::Boolean::from<8u, true>(clang::interp::Integral<8u, true>)
Unexecuted instantiation: std::__1::enable_if<(16u)!=(0), clang::interp::Boolean>::type clang::interp::Boolean::from<16u, false>(clang::interp::Integral<16u, false>)
Unexecuted instantiation: std::__1::enable_if<(16u)!=(0), clang::interp::Boolean>::type clang::interp::Boolean::from<16u, true>(clang::interp::Integral<16u, true>)
Unexecuted instantiation: std::__1::enable_if<(32u)!=(0), clang::interp::Boolean>::type clang::interp::Boolean::from<32u, false>(clang::interp::Integral<32u, false>)
Unexecuted instantiation: std::__1::enable_if<(32u)!=(0), clang::interp::Boolean>::type clang::interp::Boolean::from<32u, true>(clang::interp::Integral<32u, true>)
Unexecuted instantiation: std::__1::enable_if<(64u)!=(0), clang::interp::Boolean>::type clang::interp::Boolean::from<64u, false>(clang::interp::Integral<64u, false>)
Unexecuted instantiation: std::__1::enable_if<(64u)!=(0), clang::interp::Boolean>::type clang::interp::Boolean::from<64u, true>(clang::interp::Integral<64u, true>)
109
110
0
  static Boolean zero() { return from(false); }
111
112
  template <typename T>
113
0
  static Boolean from(T Value, unsigned NumBits) {
114
0
    return Boolean(Value);
115
0
  }
Unexecuted instantiation: clang::interp::Boolean clang::interp::Boolean::from<long>(long, unsigned int)
Unexecuted instantiation: clang::interp::Boolean clang::interp::Boolean::from<unsigned int>(unsigned int, unsigned int)
Unexecuted instantiation: clang::interp::Boolean clang::interp::Boolean::from<clang::interp::Boolean>(clang::interp::Boolean, unsigned int)
116
117
0
  static bool inRange(int64_t Value, unsigned NumBits) {
118
0
    return Value == 0 || Value == 1;
119
0
  }
120
121
0
  static bool increment(Boolean A, Boolean *R) {
122
0
    *R = Boolean(true);
123
0
    return false;
124
0
  }
125
126
0
  static bool decrement(Boolean A, Boolean *R) {
127
0
    llvm_unreachable("Cannot decrement booleans");
128
0
  }
129
130
0
  static bool add(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
131
0
    *R = Boolean(A.V || B.V);
132
0
    return false;
133
0
  }
134
135
0
  static bool sub(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
136
0
    *R = Boolean(A.V ^ B.V);
137
0
    return false;
138
0
  }
139
140
0
  static bool mul(Boolean A, Boolean B, unsigned OpBits, Boolean *R) {
141
0
    *R = Boolean(A.V && B.V);
142
0
    return false;
143
0
  }
144
145
0
  static bool inv(Boolean A, Boolean *R) {
146
0
    *R = Boolean(!A.V);
147
0
    return false;
148
0
  }
149
150
0
  static bool neg(Boolean A, Boolean *R) {
151
0
    *R = Boolean(A.V);
152
0
    return false;
153
0
  }
154
};
155
156
0
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Boolean &B) {
157
0
  B.print(OS);
158
0
  return OS;
159
0
}
160
161
}  // namespace interp
162
}  // namespace clang
163
164
#endif