2121#ifndef LLVM_SUPPORT_ALIGNMENT_H_
2222#define LLVM_SUPPORT_ALIGNMENT_H_
2323
24- #include " llvm/ADT/Optional.h"
2524#include " llvm/Support/MathExtras.h"
2625#include < cassert>
26+ #include < optional>
2727#ifndef NDEBUG
2828#include < string>
2929#endif // NDEBUG
@@ -93,14 +93,14 @@ struct Align {
9393 }
9494
9595 // / Allow constructions of constexpr Align.
96- template <size_t kValue > constexpr static LogValue Constant () {
96+ template <size_t kValue > constexpr static Align Constant () {
9797 return LogValue{static_cast <uint8_t >(CTLog2<kValue >())};
9898 }
9999
100100 // / Allow constructions of constexpr Align from types.
101101 // / 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>>();
104104 }
105105
106106 // / Constexpr constructor from LogValue type.
@@ -114,9 +114,9 @@ inline Align assumeAligned(uint64_t Value) {
114114
115115// / This struct is a compact representation of a valid (power of two) or
116116// / undefined (0) alignment.
117- struct MaybeAlign : public llvm ::Optional <Align> {
117+ struct MaybeAlign : public std ::optional <Align> {
118118private:
119- using UP = llvm::Optional <Align>;
119+ using UP = std::optional <Align>;
120120
121121public:
122122 // / Default is undefined.
@@ -128,9 +128,8 @@ struct MaybeAlign : public llvm::Optional<Align> {
128128 MaybeAlign (MaybeAlign &&Other) = default ;
129129 MaybeAlign &operator =(MaybeAlign &&Other) = default ;
130130
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) {}
134133 explicit MaybeAlign (uint64_t Value) {
135134 assert ((Value == 0 || llvm::isPowerOf2_64 (Value)) &&
136135 " Alignment is neither 0 nor a power of 2" );
@@ -292,6 +291,22 @@ bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
292291bool operator <(MaybeAlign Lhs, MaybeAlign Rhs) = delete ;
293292bool operator >(MaybeAlign Lhs, MaybeAlign Rhs) = delete ;
294293
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+
295310#ifndef NDEBUG
296311// For usage in LLVM_DEBUG macros.
297312inline std::string DebugStr (const Align &A) {
@@ -309,4 +324,4 @@ inline std::string DebugStr(const MaybeAlign &MA) {
309324
310325} // namespace llvm
311326
312- #endif // LLVM_SUPPORT_ALIGNMENT_H_
327+ #endif // LLVM_SUPPORT_ALIGNMENT_H_
0 commit comments