Skip to content

[NFC] Mark The OptionSet API constexpr #30086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions include/swift/Basic/OptionSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,45 +50,46 @@ class OptionSet {

public:
/// Create an empty option set.
OptionSet() : Storage() { }
constexpr OptionSet() : Storage() {}

/// Create an empty option set.
OptionSet(llvm::NoneType) : Storage() { }
constexpr OptionSet(llvm::NoneType) : Storage() {}

/// Create an option set with only the given option set.
OptionSet(Flags flag) : Storage(static_cast<StorageType>(flag)) { }
constexpr OptionSet(Flags flag) : Storage(static_cast<StorageType>(flag)) {}

/// Create an option set from raw storage.
explicit OptionSet(StorageType storage) : Storage(storage) { }
explicit constexpr OptionSet(StorageType storage) : Storage(storage) {}

/// Check whether an option set is non-empty.
explicit operator bool() const { return Storage != 0; }
explicit constexpr operator bool() const { return Storage != 0; }

/// Explicitly convert an option set to its underlying storage.
explicit operator StorageType() const { return Storage; }
explicit constexpr operator StorageType() const { return Storage; }

/// Explicitly convert an option set to intptr_t, for use in
/// llvm::PointerIntPair.
///
/// This member is not present if the underlying type is bigger than
/// a pointer.
template <typename T = std::intptr_t>
explicit operator typename std::enable_if<sizeof(StorageType) <= sizeof(T),
std::intptr_t>::type () const {
explicit constexpr
operator typename std::enable_if<sizeof(StorageType) <= sizeof(T),
std::intptr_t>::type() const {
return static_cast<intptr_t>(Storage);
}

/// Retrieve the "raw" representation of this option set.
StorageType toRaw() const { return Storage; }

/// Determine whether this option set contains all of the options in the
/// given set.
bool contains(OptionSet set) const {
constexpr bool contains(OptionSet set) const {
return !static_cast<bool>(set - *this);
}

/// Check if this option set contains the exact same options as the given set.
bool containsOnly(OptionSet set) const {
constexpr bool containsOnly(OptionSet set) const {
return Storage == set.Storage;
}

Expand All @@ -97,34 +98,34 @@ class OptionSet {
// want '==' behavior, use 'containsOnly'.

/// Produce the union of two option sets.
friend OptionSet operator|(OptionSet lhs, OptionSet rhs) {
friend constexpr OptionSet operator|(OptionSet lhs, OptionSet rhs) {
return OptionSet(lhs.Storage | rhs.Storage);
}

/// Produce the union of two option sets.
friend OptionSet &operator|=(OptionSet &lhs, OptionSet rhs) {
friend constexpr OptionSet &operator|=(OptionSet &lhs, OptionSet rhs) {
lhs.Storage |= rhs.Storage;
return lhs;
}
}

/// Produce the intersection of two option sets.
friend OptionSet operator&(OptionSet lhs, OptionSet rhs) {
friend constexpr OptionSet operator&(OptionSet lhs, OptionSet rhs) {
return OptionSet(lhs.Storage & rhs.Storage);
}

/// Produce the intersection of two option sets.
friend OptionSet &operator&=(OptionSet &lhs, OptionSet rhs) {
friend constexpr OptionSet &operator&=(OptionSet &lhs, OptionSet rhs) {
lhs.Storage &= rhs.Storage;
return lhs;
}

/// Produce the difference of two option sets.
friend OptionSet operator-(OptionSet lhs, OptionSet rhs) {
friend constexpr OptionSet operator-(OptionSet lhs, OptionSet rhs) {
return OptionSet(lhs.Storage & ~rhs.Storage);
}

/// Produce the difference of two option sets.
friend OptionSet &operator-=(OptionSet &lhs, OptionSet rhs) {
friend constexpr OptionSet &operator-=(OptionSet &lhs, OptionSet rhs) {
lhs.Storage &= ~rhs.Storage;
return lhs;
}
Expand Down