1//===- Optional.h - Simple variant for passing optional values --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides Optional, a template class modeled in the spirit of
10// OCaml's 'opt' variant. The idea is to strongly type whether or not
11// a value can be optional.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ADT_OPTIONAL_H
16#define LLVM_ADT_OPTIONAL_H
17
18#include "llvm/ADT/None.h"
19#include "llvm/Support/AlignOf.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/type_traits.h"
22#include <algorithm>
23#include <cassert>
24#include <new>
25#include <utility>
26
27namespace llvm {
28
29class raw_ostream;
30
31namespace optional_detail {
32/// Storage for any type.
33template <typename T, bool = is_trivially_copyable<T>::value> struct OptionalStorage {
34 AlignedCharArrayUnion<T> storage;
35 bool hasVal = false;
36
37 OptionalStorage() = default;
38
39 OptionalStorage(const T &y) : hasVal(true) { new (storage.buffer) T(y); }
40 OptionalStorage(const OptionalStorage &O) : hasVal(O.hasVal) {
41 if (hasVal)
42 new (storage.buffer) T(*O.getPointer());
43 }
44 OptionalStorage(T &&y) : hasVal(true) {
45 new (storage.buffer) T(std::forward<T>(y));
46 }
47 OptionalStorage(OptionalStorage &&O) : hasVal(O.hasVal) {
48 if (O.hasVal) {
49 new (storage.buffer) T(std::move(*O.getPointer()));
50 }
51 }
52
53 OptionalStorage &operator=(T &&y) {
54 if (hasVal)
55 *getPointer() = std::move(y);
56 else {
57 new (storage.buffer) T(std::move(y));
58 hasVal = true;
59 }
60 return *this;
61 }
62 OptionalStorage &operator=(OptionalStorage &&O) {
63 if (!O.hasVal)
64 reset();
65 else {
66 *this = std::move(*O.getPointer());
67 }
68 return *this;
69 }
70
71 // FIXME: these assignments (& the equivalent const T&/const Optional& ctors)
72 // could be made more efficient by passing by value, possibly unifying them
73 // with the rvalue versions above - but this could place a different set of
74 // requirements (notably: the existence of a default ctor) when implemented
75 // in that way. Careful SFINAE to avoid such pitfalls would be required.
76 OptionalStorage &operator=(const T &y) {
77 if (hasVal)
78 *getPointer() = y;
79 else {
80 new (storage.buffer) T(y);
81 hasVal = true;
82 }
83 return *this;
84 }
85 OptionalStorage &operator=(const OptionalStorage &O) {
86 if (!O.hasVal)
87 reset();
88 else
89 *this = *O.getPointer();
90 return *this;
91 }
92
93 ~OptionalStorage() { reset(); }
94
95 void reset() {
96 if (hasVal) {
97 (*getPointer()).~T();
98 hasVal = false;
99 }
100 }
101
102 T *getPointer() {
103 assert(hasVal);
104 return reinterpret_cast<T *>(storage.buffer);
105 }
106 const T *getPointer() const {
107 assert(hasVal);
108 return reinterpret_cast<const T *>(storage.buffer);
109 }
110};
111
112} // namespace optional_detail
113
114template <typename T> class Optional {
115 optional_detail::OptionalStorage<T> Storage;
116
117public:
118 using value_type = T;
119
120 constexpr Optional() {}
121 constexpr Optional(NoneType) {}
122
123 Optional(const T &y) : Storage(y) {}
124 Optional(const Optional &O) = default;
125
126 Optional(T &&y) : Storage(std::forward<T>(y)) {}
127 Optional(Optional &&O) = default;
128
129 Optional &operator=(T &&y) {
130 Storage = std::move(y);
131 return *this;
132 }
133 Optional &operator=(Optional &&O) = default;
134
135 /// Create a new object by constructing it in place with the given arguments.
136 template <typename... ArgTypes> void emplace(ArgTypes &&... Args) {
137 reset();
138 Storage.hasVal = true;
139 new (getPointer()) T(std::forward<ArgTypes>(Args)...);
140 }
141
142 static inline Optional create(const T *y) {
143 return y ? Optional(*y) : Optional();
144 }
145
146 Optional &operator=(const T &y) {
147 Storage = y;
148 return *this;
149 }
150 Optional &operator=(const Optional &O) = default;
151
152 void reset() { Storage.reset(); }
153
154 const T *getPointer() const {
155 assert(Storage.hasVal);
156 return reinterpret_cast<const T *>(Storage.storage.buffer);
157 }
158 T *getPointer() {
159 assert(Storage.hasVal);
160 return reinterpret_cast<T *>(Storage.storage.buffer);
161 }
162 const T &getValue() const LLVM_LVALUE_FUNCTION { return *getPointer(); }
163 T &getValue() LLVM_LVALUE_FUNCTION { return *getPointer(); }
164
165 explicit operator bool() const { return Storage.hasVal; }
166 bool hasValue() const { return Storage.hasVal; }
167 const T *operator->() const { return getPointer(); }
168 T *operator->() { return getPointer(); }
169 const T &operator*() const LLVM_LVALUE_FUNCTION { return *getPointer(); }
170 T &operator*() LLVM_LVALUE_FUNCTION { return *getPointer(); }
171
172 template <typename U>
173 constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
174 return hasValue() ? getValue() : std::forward<U>(value);
175 }
176
177#if LLVM_HAS_RVALUE_REFERENCE_THIS
178 T &&getValue() && { return std::move(*getPointer()); }
179 T &&operator*() && { return std::move(*getPointer()); }
180
181 template <typename U>
182 T getValueOr(U &&value) && {
183 return hasValue() ? std::move(getValue()) : std::forward<U>(value);
184 }
185#endif
186};
187
188template <typename T, typename U>
189bool operator==(const Optional<T> &X, const Optional<U> &Y) {
190 if (X && Y)
191 return *X == *Y;
192 return X.hasValue() == Y.hasValue();
193}
194
195template <typename T, typename U>
196bool operator!=(const Optional<T> &X, const Optional<U> &Y) {
197 return !(X == Y);
198}
199
200template <typename T, typename U>
201bool operator<(const Optional<T> &X, const Optional<U> &Y) {
202 if (X && Y)
203 return *X < *Y;
204 return X.hasValue() < Y.hasValue();
205}
206
207template <typename T, typename U>
208bool operator<=(const Optional<T> &X, const Optional<U> &Y) {
209 return !(Y < X);
210}
211
212template <typename T, typename U>
213bool operator>(const Optional<T> &X, const Optional<U> &Y) {
214 return Y < X;
215}
216
217template <typename T, typename U>
218bool operator>=(const Optional<T> &X, const Optional<U> &Y) {
219 return !(X < Y);
220}
221
222template<typename T>
223bool operator==(const Optional<T> &X, NoneType) {
224 return !X;
225}
226
227template<typename T>
228bool operator==(NoneType, const Optional<T> &X) {
229 return X == None;
230}
231
232template<typename T>
233bool operator!=(const Optional<T> &X, NoneType) {
234 return !(X == None);
235}
236
237template<typename T>
238bool operator!=(NoneType, const Optional<T> &X) {
239 return X != None;
240}
241
242template <typename T> bool operator<(const Optional<T> &X, NoneType) {
243 return false;
244}
245
246template <typename T> bool operator<(NoneType, const Optional<T> &X) {
247 return X.hasValue();
248}
249
250template <typename T> bool operator<=(const Optional<T> &X, NoneType) {
251 return !(None < X);
252}
253
254template <typename T> bool operator<=(NoneType, const Optional<T> &X) {
255 return !(X < None);
256}
257
258template <typename T> bool operator>(const Optional<T> &X, NoneType) {
259 return None < X;
260}
261
262template <typename T> bool operator>(NoneType, const Optional<T> &X) {
263 return X < None;
264}
265
266template <typename T> bool operator>=(const Optional<T> &X, NoneType) {
267 return None <= X;
268}
269
270template <typename T> bool operator>=(NoneType, const Optional<T> &X) {
271 return X <= None;
272}
273
274template <typename T> bool operator==(const Optional<T> &X, const T &Y) {
275 return X && *X == Y;
276}
277
278template <typename T> bool operator==(const T &X, const Optional<T> &Y) {
279 return Y && X == *Y;
280}
281
282template <typename T> bool operator!=(const Optional<T> &X, const T &Y) {
283 return !(X == Y);
284}
285
286template <typename T> bool operator!=(const T &X, const Optional<T> &Y) {
287 return !(X == Y);
288}
289
290template <typename T> bool operator<(const Optional<T> &X, const T &Y) {
291 return !X || *X < Y;
292}
293
294template <typename T> bool operator<(const T &X, const Optional<T> &Y) {
295 return Y && X < *Y;
296}
297
298template <typename T> bool operator<=(const Optional<T> &X, const T &Y) {
299 return !(Y < X);
300}
301
302template <typename T> bool operator<=(const T &X, const Optional<T> &Y) {
303 return !(Y < X);
304}
305
306template <typename T> bool operator>(const Optional<T> &X, const T &Y) {
307 return Y < X;
308}
309
310template <typename T> bool operator>(const T &X, const Optional<T> &Y) {
311 return Y < X;
312}
313
314template <typename T> bool operator>=(const Optional<T> &X, const T &Y) {
315 return !(X < Y);
316}
317
318template <typename T> bool operator>=(const T &X, const Optional<T> &Y) {
319 return !(X < Y);
320}
321
322raw_ostream &operator<<(raw_ostream &OS, NoneType);
323
324template <typename T, typename = decltype(std::declval<raw_ostream &>()
325 << std::declval<const T &>())>
326raw_ostream &operator<<(raw_ostream &OS, const Optional<T> &O) {
327 if (O)
328 OS << *O;
329 else
330 OS << None;
331 return OS;
332}
333
334} // end namespace llvm
335
336#endif // LLVM_ADT_OPTIONAL_H
337