-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[ADT] Add and use (for AArch64) ValueOrSentinel<T, Sentinel>
#158120
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
Open
MacDue
wants to merge
9
commits into
llvm:main
Choose a base branch
from
MacDue:sentinel_value
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−61
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6f14b6d
[ADT] Add and use (for AArch64) `ValueWithSentinel<T, Sentinel>`
MacDue 13c85fd
Fixups
MacDue 0b1db69
Fixups
MacDue 9ea7165
Rename
MacDue b52e4c0
Support adjusted values
MacDue 82b9d71
Undo test name change
MacDue 421c7e0
Format
MacDue e450ef0
Mark some more methods as constexpr
MacDue c0f645e
Fixups
MacDue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// This file defines the ValueOrSentinel class, which is a type akin to a | ||
/// std::optional, but uses a sentinel rather than an additional "valid" flag. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_ADT_VALUEORSENTINEL_H | ||
#define LLVM_ADT_VALUEORSENTINEL_H | ||
|
||
#include <cassert> | ||
#include <limits> | ||
#include <optional> | ||
#include <utility> | ||
|
||
namespace llvm { | ||
|
||
namespace detail { | ||
/// An adjustment allows changing how the value is stored. An example use case | ||
/// is to use zero as a sentinel value. | ||
template <typename T> struct DefaultValueAdjustment { | ||
constexpr static T toRepresentation(T Value) { return Value; } | ||
constexpr static T fromRepresentation(T Value) { return Value; } | ||
}; | ||
} // namespace detail | ||
|
||
template <typename T, T Sentinel, | ||
typename Adjust = detail::DefaultValueAdjustment<T>> | ||
class ValueOrSentinel { | ||
public: | ||
constexpr ValueOrSentinel() = default; | ||
constexpr ValueOrSentinel(std::nullopt_t) {}; | ||
constexpr ValueOrSentinel(T Value) : Value(Adjust::toRepresentation(Value)) { | ||
assert(this->Value != Sentinel && | ||
"Value is sentinel (use default constructor)"); | ||
}; | ||
|
||
constexpr ValueOrSentinel &operator=(T NewValue) { | ||
Value = Adjust::toRepresentation(NewValue); | ||
assert(Value != Sentinel && "NewValue is sentinel (use .clear())"); | ||
return *this; | ||
} | ||
|
||
constexpr bool operator==(ValueOrSentinel Other) const { | ||
return Value == Other.Value; | ||
} | ||
constexpr bool operator!=(ValueOrSentinel Other) const { | ||
return !(*this == Other); | ||
} | ||
|
||
T value() const { | ||
assert(has_value() && ".value() called on sentinel"); | ||
return Adjust::fromRepresentation(Value); | ||
} | ||
T operator*() const { return value(); } | ||
|
||
explicit operator T() const { return value(); } | ||
explicit constexpr operator bool() const { return has_value(); } | ||
|
||
constexpr void clear() { Value = Sentinel; } | ||
constexpr bool has_value() const { return Value != Sentinel; } | ||
|
||
constexpr static ValueOrSentinel fromInternalRepresentation(unsigned Value) { | ||
return {std::nullopt, Value}; | ||
} | ||
constexpr T toInternalRepresentation() const { return Value; } | ||
|
||
protected: | ||
ValueOrSentinel(std::nullopt_t, T Value) : Value(Value) {}; | ||
|
||
T Value{Sentinel}; | ||
}; | ||
|
||
template <typename T> | ||
using ValueOrSentinelIntMax = ValueOrSentinel<T, std::numeric_limits<T>::max()>; | ||
|
||
} // namespace llvm | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/ADT/ValueOrSentinel.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
|
||
TEST(ValueOrSentinelTest, Basic) { | ||
// Default constructor should equal sentinel. | ||
ValueOrSentinelIntMax<int> Value; | ||
EXPECT_FALSE(Value.has_value()); | ||
EXPECT_FALSE(bool(Value)); | ||
|
||
// Assignment operator. | ||
Value = 1000; | ||
EXPECT_TRUE(Value.has_value()); | ||
|
||
// .value(), operator*, implicit constructor, explicit conversion | ||
EXPECT_EQ(Value, 1000); | ||
EXPECT_EQ(Value.value(), 1000); | ||
EXPECT_EQ(*Value, 1000); | ||
EXPECT_EQ(int(Value), 1000); | ||
|
||
// .clear() should set value to sentinel | ||
Value.clear(); | ||
EXPECT_FALSE(Value.has_value()); | ||
EXPECT_FALSE(bool(Value)); | ||
|
||
// construction from value, comparison operators | ||
ValueOrSentinelIntMax<int> OtherValue(99); | ||
EXPECT_TRUE(OtherValue.has_value()); | ||
EXPECT_TRUE(bool(OtherValue)); | ||
EXPECT_EQ(OtherValue, 99); | ||
EXPECT_NE(Value, OtherValue); | ||
|
||
Value = OtherValue; | ||
EXPECT_EQ(Value, OtherValue); | ||
} | ||
|
||
TEST(ValueOrSentinelTest, PointerType) { | ||
ValueOrSentinel<int *, nullptr> Value; | ||
EXPECT_FALSE(Value.has_value()); | ||
|
||
int A = 10; | ||
Value = &A; | ||
EXPECT_TRUE(Value.has_value()); | ||
|
||
EXPECT_EQ(*Value.value(), 10); | ||
|
||
Value.clear(); | ||
EXPECT_FALSE(Value.has_value()); | ||
} | ||
|
||
} // end anonymous namespace |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.