Bug 1451722 - Constexpr-ify mozilla::Span more. r=hsivonen
MozReview-Commit-ID: B7sxH5BAdp6
--- a/mfbt/Span.h
+++ b/mfbt/Span.h
@@ -28,33 +28,16 @@
#include "mozilla/TypeTraits.h"
#include "mozilla/UniquePtr.h"
#include <algorithm>
#include <array>
#include <cstring>
#include <iterator>
-// Classifications for reasons why constexpr was removed in C++14 to C++11
-// conversion. Once we upgrade compilers, we can try defining each of these
-// to constexpr to restore a category of constexprs at a time.
-#if !defined(__clang__) && defined(__GNUC__) && __cpp_constexpr < 201304
-#define MOZ_SPAN_ASSERTION_CONSTEXPR
-#define MOZ_SPAN_GCC_CONSTEXPR
-#define MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR
-#define MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN
-#define MOZ_SPAN_NON_CONST_CONSTEXPR
-#else
-#define MOZ_SPAN_ASSERTION_CONSTEXPR constexpr
-#define MOZ_SPAN_GCC_CONSTEXPR constexpr
-#define MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR constexpr
-#define MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN constexpr
-#define MOZ_SPAN_NON_CONST_CONSTEXPR constexpr
-#endif
-
#ifdef _MSC_VER
#pragma warning(push)
// turn off some warnings that are noisy about our MOZ_RELEASE_ASSERT statements
#pragma warning(disable : 4127) // conditional expression is constant
// blanket turn off warnings from CppCoreCheck for now
// so people aren't annoyed by them when running the tool.
@@ -170,82 +153,82 @@ public:
using value_type = remove_const_t<element_type_>;
using difference_type = typename Span::index_type;
using reference = conditional_t<IsConst, const element_type_, element_type_>&;
using pointer = add_pointer_t<reference>;
constexpr span_iterator() : span_iterator(nullptr, 0) {}
- MOZ_SPAN_ASSERTION_CONSTEXPR span_iterator(const Span* span,
+ constexpr span_iterator(const Span* span,
typename Span::index_type index)
: span_(span)
, index_(index)
{
MOZ_RELEASE_ASSERT(span == nullptr ||
(index_ >= 0 && index <= span_->Length()));
}
friend class span_iterator<Span, true>;
constexpr MOZ_IMPLICIT span_iterator(const span_iterator<Span, false>& other)
: span_iterator(other.span_, other.index_)
{
}
- MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR span_iterator<Span, IsConst>&
+ constexpr span_iterator<Span, IsConst>&
operator=(const span_iterator<Span, IsConst>&) = default;
- MOZ_SPAN_GCC_CONSTEXPR reference operator*() const
+ constexpr reference operator*() const
{
MOZ_RELEASE_ASSERT(span_);
return (*span_)[index_];
}
constexpr pointer operator->() const
{
MOZ_RELEASE_ASSERT(span_);
return &((*span_)[index_]);
}
- MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator& operator++()
+ constexpr span_iterator& operator++()
{
MOZ_RELEASE_ASSERT(span_ && index_ >= 0 && index_ < span_->Length());
++index_;
return *this;
}
constexpr span_iterator operator++(int)
{
auto ret = *this;
++(*this);
return ret;
}
- MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator& operator--()
+ constexpr span_iterator& operator--()
{
MOZ_RELEASE_ASSERT(span_ && index_ > 0 && index_ <= span_->Length());
--index_;
return *this;
}
constexpr span_iterator operator--(int)
{
auto ret = *this;
--(*this);
return ret;
}
- MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN span_iterator
+ constexpr span_iterator
operator+(difference_type n) const
{
auto ret = *this;
return ret += n;
}
- MOZ_SPAN_GCC_CONSTEXPR span_iterator& operator+=(difference_type n)
+ constexpr span_iterator& operator+=(difference_type n)
{
MOZ_RELEASE_ASSERT(span_ && (index_ + n) >= 0 &&
(index_ + n) <= span_->Length());
index_ += n;
return *this;
}
constexpr span_iterator
@@ -256,17 +239,17 @@ public:
}
constexpr span_iterator& operator-=(difference_type n)
{
return *this += -n;
}
- MOZ_SPAN_GCC_CONSTEXPR difference_type
+ constexpr difference_type
operator-(const span_iterator& rhs) const
{
MOZ_RELEASE_ASSERT(span_ == rhs.span_);
return index_ - rhs.index_;
}
constexpr reference operator[](difference_type n) const
{
@@ -280,17 +263,17 @@ public:
}
constexpr friend bool operator!=(const span_iterator& lhs,
const span_iterator& rhs)
{
return !(lhs == rhs);
}
- MOZ_SPAN_GCC_CONSTEXPR friend bool operator<(const span_iterator& lhs,
+ constexpr friend bool operator<(const span_iterator& lhs,
const span_iterator& rhs)
{
MOZ_RELEASE_ASSERT(lhs.span_ == rhs.span_);
return lhs.index_ < rhs.index_;
}
constexpr friend bool operator<=(const span_iterator& lhs,
const span_iterator& rhs)
@@ -335,25 +318,25 @@ class extent_type
public:
using index_type = size_t;
static_assert(Ext >= 0, "A fixed-size Span must be >= 0 in size.");
constexpr extent_type() {}
template<index_type Other>
- MOZ_SPAN_ASSERTION_CONSTEXPR MOZ_IMPLICIT extent_type(extent_type<Other> ext)
+ constexpr MOZ_IMPLICIT extent_type(extent_type<Other> ext)
{
static_assert(
Other == Ext || Other == dynamic_extent,
"Mismatch between fixed-size extent and size of initializing data.");
MOZ_RELEASE_ASSERT(ext.size() == Ext);
}
- MOZ_SPAN_ASSERTION_CONSTEXPR MOZ_IMPLICIT extent_type(index_type length)
+ constexpr MOZ_IMPLICIT extent_type(index_type length)
{
MOZ_RELEASE_ASSERT(length == Ext);
}
constexpr index_type size() const { return Ext; }
};
template<>
@@ -641,20 +624,20 @@ public:
element_type>::value>>
constexpr MOZ_IMPLICIT Span(Span<OtherElementType, OtherExtent>&& other)
: storage_(other.data(),
span_details::extent_type<OtherExtent>(other.size()))
{
}
~Span() = default;
- MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR Span& operator=(const Span& other)
+ constexpr Span& operator=(const Span& other)
= default;
- MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR Span& operator=(Span&& other)
+ constexpr Span& operator=(Span&& other)
= default;
// [Span.sub], Span subviews
/**
* Subspan with first N elements with compile-time N.
*/
template<size_t Count>
constexpr Span<element_type, Count> First() const
@@ -848,17 +831,17 @@ private:
// this implementation detail class lets us take advantage of the
// empty base class optimization to pay for only storage of a single
// pointer in the case of fixed-size Spans
template<class ExtentType>
class storage_type : public ExtentType
{
public:
template<class OtherExtentType>
- MOZ_SPAN_ASSERTION_CONSTEXPR storage_type(pointer elements,
+ constexpr storage_type(pointer elements,
OtherExtentType ext)
: ExtentType(ext)
// Replace nullptr with 0x1 for Rust slice compatibility. See
// https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
, data_(elements ? elements : reinterpret_cast<pointer>(0x1))
{
const size_t extentSize = ExtentType::size();
MOZ_RELEASE_ASSERT(
@@ -1094,15 +1077,9 @@ MakeStringSpan(const char16_t* aZeroTerm
#undef constexpr
#pragma pop_macro("constexpr")
#endif // _MSC_VER < 1910
#pragma warning(pop)
#endif // _MSC_VER
-#undef MOZ_SPAN_ASSERTION_CONSTEXPR
-#undef MOZ_SPAN_GCC_CONSTEXPR
-#undef MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR
-#undef MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN
-#undef MOZ_SPAN_NON_CONST_CONSTEXPR
-
#endif // mozilla_Span_h