From c7987d49483685a29c3004c8710011a35cbb53e1 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sun, 19 Jun 2022 18:34:33 -0700 Subject: [PATCH] [ADT] Use value instead of getValue() (NFC) Since Optional uses a custom storage class, this patch adds value to MapEntryOptionalStorage. --- clang/include/clang/Basic/DirectoryEntry.h | 12 +++++++ llvm/include/llvm/ADT/Optional.h | 38 +++++++++++----------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/clang/include/clang/Basic/DirectoryEntry.h b/clang/include/clang/Basic/DirectoryEntry.h index 0cfdb1d73eef..d54b81d2e6e7 100644 --- a/clang/include/clang/Basic/DirectoryEntry.h +++ b/clang/include/clang/Basic/DirectoryEntry.h @@ -133,14 +133,26 @@ public: bool has_value() const { return MaybeRef.hasOptionalValue(); } bool hasValue() const { return MaybeRef.hasOptionalValue(); } + RefTy &value() & { + assert(has_value()); + return MaybeRef; + } RefTy &getValue() & { assert(hasValue()); return MaybeRef; } + RefTy const &value() const & { + assert(has_value()); + return MaybeRef; + } RefTy const &getValue() const & { assert(hasValue()); return MaybeRef; } + RefTy &&value() && { + assert(has_value()); + return std::move(MaybeRef); + } RefTy &&getValue() && { assert(hasValue()); return std::move(MaybeRef); diff --git a/llvm/include/llvm/ADT/Optional.h b/llvm/include/llvm/ADT/Optional.h index 62765914eb66..d1615d903e98 100644 --- a/llvm/include/llvm/ADT/Optional.h +++ b/llvm/include/llvm/ADT/Optional.h @@ -300,53 +300,53 @@ public: void reset() { Storage.reset(); } - constexpr const T *getPointer() const { return &Storage.getValue(); } - T *getPointer() { return &Storage.getValue(); } - constexpr const T &value() const & { return Storage.getValue(); } - constexpr const T &getValue() const & { return Storage.getValue(); } - T &value() & { return Storage.getValue(); } - T &getValue() & { return Storage.getValue(); } + constexpr const T *getPointer() const { return &Storage.value(); } + T *getPointer() { return &Storage.value(); } + constexpr const T &value() const & { return Storage.value(); } + constexpr const T &getValue() const & { return Storage.value(); } + T &value() & { return Storage.value(); } + T &getValue() & { return Storage.value(); } constexpr explicit operator bool() const { return has_value(); } constexpr bool has_value() const { return Storage.has_value(); } constexpr bool hasValue() const { return Storage.has_value(); } constexpr const T *operator->() const { return getPointer(); } T *operator->() { return getPointer(); } - constexpr const T &operator*() const & { return getValue(); } - T &operator*() & { return getValue(); } + constexpr const T &operator*() const & { return value(); } + T &operator*() & { return value(); } template constexpr T value_or(U &&alt) const & { - return has_value() ? getValue() : std::forward(alt); + return has_value() ? value() : std::forward(alt); } template constexpr T getValueOr(U &&alt) const & { - return has_value() ? getValue() : std::forward(alt); + return has_value() ? value() : std::forward(alt); } /// Apply a function to the value if present; otherwise return None. template - auto map(const Function &F) const & -> Optional { + auto map(const Function &F) const & -> Optional { if (*this) - return F(getValue()); + return F(value()); return None; } - T &&value() && { return std::move(Storage.getValue()); } - T &&getValue() && { return std::move(Storage.getValue()); } - T &&operator*() && { return std::move(Storage.getValue()); } + T &&value() && { return std::move(Storage.value()); } + T &&getValue() && { return std::move(Storage.value()); } + T &&operator*() && { return std::move(Storage.value()); } template T value_or(U &&alt) && { - return has_value() ? std::move(getValue()) : std::forward(alt); + return has_value() ? std::move(value()) : std::forward(alt); } template T getValueOr(U &&alt) && { - return has_value() ? std::move(getValue()) : std::forward(alt); + return has_value() ? std::move(value()) : std::forward(alt); } /// Apply a function to the value if present; otherwise return None. template auto map(const Function &F) - && -> Optional { + && -> Optional { if (*this) - return F(std::move(*this).getValue()); + return F(std::move(*this).value()); return None; } };