21
21
#ifndef LLVM_SUPPORT_ALIGNMENT_H_
22
22
#define LLVM_SUPPORT_ALIGNMENT_H_
23
23
24
- #include " llvm/ADT/Optional.h"
25
24
#include " llvm/Support/MathExtras.h"
26
25
#include < cassert>
26
+ #include < optional>
27
27
#ifndef NDEBUG
28
28
#include < string>
29
29
#endif // NDEBUG
@@ -93,14 +93,14 @@ struct Align {
93
93
}
94
94
95
95
// / Allow constructions of constexpr Align.
96
- template <size_t kValue > constexpr static LogValue Constant () {
96
+ template <size_t kValue > constexpr static Align Constant () {
97
97
return LogValue{static_cast <uint8_t >(CTLog2<kValue >())};
98
98
}
99
99
100
100
// / Allow constructions of constexpr Align from types.
101
101
// / Compile time equivalent to Align(alignof(T)).
102
- template <typename T> constexpr static LogValue Of () {
103
- return Constant<std::alignment_of <T>::value >();
102
+ template <typename T> constexpr static Align Of () {
103
+ return Constant<std::alignment_of_v <T>>();
104
104
}
105
105
106
106
// / Constexpr constructor from LogValue type.
@@ -114,9 +114,9 @@ inline Align assumeAligned(uint64_t Value) {
114
114
115
115
// / This struct is a compact representation of a valid (power of two) or
116
116
// / undefined (0) alignment.
117
- struct MaybeAlign : public llvm ::Optional <Align> {
117
+ struct MaybeAlign : public std ::optional <Align> {
118
118
private:
119
- using UP = llvm::Optional <Align>;
119
+ using UP = std::optional <Align>;
120
120
121
121
public:
122
122
// / Default is undefined.
@@ -128,9 +128,8 @@ struct MaybeAlign : public llvm::Optional<Align> {
128
128
MaybeAlign (MaybeAlign &&Other) = default ;
129
129
MaybeAlign &operator =(MaybeAlign &&Other) = default ;
130
130
131
- // / Use llvm::Optional<Align> constructor.
132
- using UP::UP;
133
-
131
+ constexpr MaybeAlign (std::nullopt_t None) : UP(None) {}
132
+ constexpr MaybeAlign (Align Value) : UP(Value) {}
134
133
explicit MaybeAlign (uint64_t Value) {
135
134
assert ((Value == 0 || llvm::isPowerOf2_64 (Value)) &&
136
135
" Alignment is neither 0 nor a power of 2" );
@@ -292,6 +291,22 @@ bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
292
291
bool operator <(MaybeAlign Lhs, MaybeAlign Rhs) = delete ;
293
292
bool operator >(MaybeAlign Lhs, MaybeAlign Rhs) = delete ;
294
293
294
+ // Allow equality comparisons between Align and MaybeAlign.
295
+ inline bool operator ==(MaybeAlign Lhs, Align Rhs) { return Lhs && *Lhs == Rhs; }
296
+ inline bool operator !=(MaybeAlign Lhs, Align Rhs) { return !(Lhs == Rhs); }
297
+ inline bool operator ==(Align Lhs, MaybeAlign Rhs) { return Rhs == Lhs; }
298
+ inline bool operator !=(Align Lhs, MaybeAlign Rhs) { return !(Rhs == Lhs); }
299
+ // Allow equality comparisons with MaybeAlign.
300
+ inline bool operator ==(MaybeAlign Lhs, MaybeAlign Rhs) {
301
+ return (Lhs && Rhs && (*Lhs == *Rhs)) || (!Lhs && !Rhs);
302
+ }
303
+ inline bool operator !=(MaybeAlign Lhs, MaybeAlign Rhs) { return !(Lhs == Rhs); }
304
+ // Allow equality comparisons with std::nullopt.
305
+ inline bool operator ==(MaybeAlign Lhs, std::nullopt_t ) { return !bool (Lhs); }
306
+ inline bool operator !=(MaybeAlign Lhs, std::nullopt_t ) { return bool (Lhs); }
307
+ inline bool operator ==(std::nullopt_t , MaybeAlign Rhs) { return !bool (Rhs); }
308
+ inline bool operator !=(std::nullopt_t , MaybeAlign Rhs) { return bool (Rhs); }
309
+
295
310
#ifndef NDEBUG
296
311
// For usage in LLVM_DEBUG macros.
297
312
inline std::string DebugStr (const Align &A) {
@@ -309,4 +324,4 @@ inline std::string DebugStr(const MaybeAlign &MA) {
309
324
310
325
} // namespace llvm
311
326
312
- #endif // LLVM_SUPPORT_ALIGNMENT_H_
327
+ #endif // LLVM_SUPPORT_ALIGNMENT_H_
0 commit comments