Skip to content

Commit 567b68a

Browse files
committed
stdlib: adapt to llvm's changed Optional API
add `value()` which replaces the deprecated `getValue()`
1 parent 096f02b commit 567b68a

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

stdlib/include/llvm/ADT/Optional.h

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ template <typename T, bool = (llvm::is_trivially_copy_constructible<T>::value &&
7272
class OptionalStorage {
7373
union {
7474
char empty;
75-
T value;
75+
T val;
7676
};
7777
bool hasVal;
7878

@@ -83,22 +83,22 @@ class OptionalStorage {
8383

8484
constexpr OptionalStorage(OptionalStorage const &other) : OptionalStorage() {
8585
if (other.hasValue()) {
86-
emplace(other.value);
86+
emplace(other.val);
8787
}
8888
}
8989
constexpr OptionalStorage(OptionalStorage &&other) : OptionalStorage() {
9090
if (other.hasValue()) {
91-
emplace(std::move(other.value));
91+
emplace(std::move(other.val));
9292
}
9393
}
9494

9595
template <class... Args>
9696
constexpr explicit OptionalStorage(in_place_t, Args &&... args)
97-
: value(std::forward<Args>(args)...), hasVal(true) {}
97+
: val(std::forward<Args>(args)...), hasVal(true) {}
9898

9999
void reset() noexcept {
100100
if (hasVal) {
101-
value.~T();
101+
val.~T();
102102
hasVal = false;
103103
}
104104
}
@@ -107,39 +107,39 @@ class OptionalStorage {
107107

108108
T &getValue() LLVM_LVALUE_FUNCTION noexcept {
109109
assert(hasVal);
110-
return value;
110+
return val;
111111
}
112112
constexpr T const &getValue() const LLVM_LVALUE_FUNCTION noexcept {
113113
assert(hasVal);
114-
return value;
114+
return val;
115115
}
116116
#if LLVM_HAS_RVALUE_REFERENCE_THIS
117117
T &&getValue() && noexcept {
118118
assert(hasVal);
119-
return std::move(value);
119+
return std::move(val);
120120
}
121121
#endif
122122

123123
template <class... Args> void emplace(Args &&... args) {
124124
reset();
125-
::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
125+
::new ((void *)std::addressof(val)) T(std::forward<Args>(args)...);
126126
hasVal = true;
127127
}
128128

129129
OptionalStorage &operator=(T const &y) {
130130
if (hasValue()) {
131-
value = y;
131+
val = y;
132132
} else {
133-
::new ((void *)std::addressof(value)) T(y);
133+
::new ((void *)std::addressof(val)) T(y);
134134
hasVal = true;
135135
}
136136
return *this;
137137
}
138138
OptionalStorage &operator=(T &&y) {
139139
if (hasValue()) {
140-
value = std::move(y);
140+
val = std::move(y);
141141
} else {
142-
::new ((void *)std::addressof(value)) T(std::move(y));
142+
::new ((void *)std::addressof(val)) T(std::move(y));
143143
hasVal = true;
144144
}
145145
return *this;
@@ -148,9 +148,9 @@ class OptionalStorage {
148148
OptionalStorage &operator=(OptionalStorage const &other) {
149149
if (other.hasValue()) {
150150
if (hasValue()) {
151-
value = other.value;
151+
val = other.val;
152152
} else {
153-
::new ((void *)std::addressof(value)) T(other.value);
153+
::new ((void *)std::addressof(val)) T(other.val);
154154
hasVal = true;
155155
}
156156
} else {
@@ -162,9 +162,9 @@ class OptionalStorage {
162162
OptionalStorage &operator=(OptionalStorage &&other) {
163163
if (other.hasValue()) {
164164
if (hasValue()) {
165-
value = std::move(other.value);
165+
val = std::move(other.val);
166166
} else {
167-
::new ((void *)std::addressof(value)) T(std::move(other.value));
167+
::new ((void *)std::addressof(val)) T(std::move(other.val));
168168
hasVal = true;
169169
}
170170
} else {
@@ -177,7 +177,7 @@ class OptionalStorage {
177177
template <typename T> class OptionalStorage<T, true> {
178178
union {
179179
char empty;
180-
T value;
180+
T val;
181181
};
182182
bool hasVal = false;
183183

@@ -194,11 +194,11 @@ template <typename T> class OptionalStorage<T, true> {
194194

195195
template <class... Args>
196196
constexpr explicit OptionalStorage(in_place_t, Args &&... args)
197-
: value(std::forward<Args>(args)...), hasVal(true) {}
197+
: val(std::forward<Args>(args)...), hasVal(true) {}
198198

199199
void reset() noexcept {
200200
if (hasVal) {
201-
value.~T();
201+
val.~T();
202202
hasVal = false;
203203
}
204204
}
@@ -207,39 +207,39 @@ template <typename T> class OptionalStorage<T, true> {
207207

208208
T &getValue() LLVM_LVALUE_FUNCTION noexcept {
209209
assert(hasVal);
210-
return value;
210+
return val;
211211
}
212212
constexpr T const &getValue() const LLVM_LVALUE_FUNCTION noexcept {
213213
assert(hasVal);
214-
return value;
214+
return val;
215215
}
216216
#if LLVM_HAS_RVALUE_REFERENCE_THIS
217217
T &&getValue() && noexcept {
218218
assert(hasVal);
219-
return std::move(value);
219+
return std::move(val);
220220
}
221221
#endif
222222

223223
template <class... Args> void emplace(Args &&... args) {
224224
reset();
225-
::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
225+
::new ((void *)std::addressof(val)) T(std::forward<Args>(args)...);
226226
hasVal = true;
227227
}
228228

229229
OptionalStorage &operator=(T const &y) {
230230
if (hasValue()) {
231-
value = y;
231+
val = y;
232232
} else {
233-
::new ((void *)std::addressof(value)) T(y);
233+
::new ((void *)std::addressof(val)) T(y);
234234
hasVal = true;
235235
}
236236
return *this;
237237
}
238238
OptionalStorage &operator=(T &&y) {
239239
if (hasValue()) {
240-
value = std::move(y);
240+
val = std::move(y);
241241
} else {
242-
::new ((void *)std::addressof(value)) T(std::move(y));
242+
::new ((void *)std::addressof(val)) T(std::move(y));
243243
hasVal = true;
244244
}
245245
return *this;
@@ -292,9 +292,13 @@ template <typename T> class Optional {
292292

293293
constexpr const T *getPointer() const { return &Storage.getValue(); }
294294
T *getPointer() { return &Storage.getValue(); }
295+
constexpr const T &value() const LLVM_LVALUE_FUNCTION {
296+
return Storage.getValue();
297+
}
295298
constexpr const T &getValue() const LLVM_LVALUE_FUNCTION {
296299
return Storage.getValue();
297300
}
301+
T &value() LLVM_LVALUE_FUNCTION { return Storage.getValue(); }
298302
T &getValue() LLVM_LVALUE_FUNCTION { return Storage.getValue(); }
299303

300304
constexpr explicit operator bool() const { return hasValue(); }
@@ -307,8 +311,8 @@ template <typename T> class Optional {
307311
T &operator*() LLVM_LVALUE_FUNCTION { return getValue(); }
308312

309313
template <typename U>
310-
constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
311-
return hasValue() ? getValue() : std::forward<U>(value);
314+
constexpr T getValueOr(U &&alt) const LLVM_LVALUE_FUNCTION {
315+
return hasValue() ? getValue() : std::forward<U>(alt);
312316
}
313317

314318
/// Apply a function to the value if present; otherwise return None.
@@ -320,12 +324,13 @@ template <typename T> class Optional {
320324
}
321325

322326
#if LLVM_HAS_RVALUE_REFERENCE_THIS
327+
T &&value() && { return std::move(Storage.getValue()); }
323328
T &&getValue() && { return std::move(Storage.getValue()); }
324329
T &&operator*() && { return std::move(Storage.getValue()); }
325330

326331
template <typename U>
327-
T getValueOr(U &&value) && {
328-
return hasValue() ? std::move(getValue()) : std::forward<U>(value);
332+
T getValueOr(U &&alt) && {
333+
return hasValue() ? std::move(getValue()) : std::forward<U>(alt);
329334
}
330335

331336
/// Apply a function to the value if present; otherwise return None.

0 commit comments

Comments
 (0)