Skip to content

Commit 9891c7f

Browse files
committed
Replace swift::Optional with llvm::Optional.
There are some compatibility aliases here that will go away after the switch. Swift SVN r22474
1 parent e999f40 commit 9891c7f

File tree

4 files changed

+7
-169
lines changed

4 files changed

+7
-169
lines changed

include/swift/Basic/Optional.h

+4-167
Original file line numberDiff line numberDiff line change
@@ -25,176 +25,13 @@
2525
#define SWIFT_BASIC_OPTIONAL_H
2626

2727
#include "llvm/ADT/Optional.h"
28-
#include <type_traits>
29-
#include <utility>
30-
#include <cassert>
3128

3229
namespace swift {
33-
/// An enum whose purpose is to make it easier to initialize an
34-
/// empty optional.
35-
static const enum class Nothing_t { Nothing } Nothing = Nothing_t::Nothing;
30+
static auto Nothing = llvm::None;
31+
using Nothing_t = decltype(Nothing);
3632

37-
static_assert(!std::is_convertible<Nothing_t, bool>::value,
38-
"Nothing must not be implicitly convertible to bool");
39-
40-
template<typename T>
41-
class Optional {
42-
// Place Value in an anonymous union to suppress implicit value semantics.
43-
union {
44-
T Value;
45-
};
46-
unsigned HasValue : 1;
47-
48-
public:
49-
typedef T value_type;
50-
51-
/// \brief Construct an empty instance.
52-
Optional() : HasValue(false) { }
53-
54-
/// \brief Construct an empty instance.
55-
Optional(Nothing_t _) : HasValue(false) { }
56-
57-
/// \brief Construct an instance containing a value of type \c T
58-
/// constructed with the given arguments.
59-
///
60-
/// \param Args The arguments with which the \c T object will be
61-
/// direct-initialized.
62-
template<typename ...ArgTypes>
63-
Optional(ArgTypes &&...Args)
64-
: Value(std::forward<ArgTypes>(Args)...), HasValue(true)
65-
{
66-
}
67-
68-
Optional(const llvm::Optional<T> &other) : HasValue(false) {
69-
if (other)
70-
emplace(*other);
71-
}
72-
73-
Optional(llvm::Optional<T> &&other) : HasValue(false) {
74-
if (other)
75-
emplace(std::move(*other));
76-
}
77-
78-
Optional(Optional &Other) : HasValue(Other.HasValue) {
79-
if (HasValue)
80-
::new ((void*)&Value) T(Other.Value);
81-
}
82-
83-
Optional(const Optional &Other) : HasValue(Other.HasValue) {
84-
if (HasValue)
85-
::new ((void*)&Value) T(Other.Value);
86-
}
87-
88-
Optional(Optional &&Other) : HasValue(Other.HasValue) {
89-
if (HasValue) {
90-
::new ((void*)&Value) T(std::move(Other.Value));
91-
Other.HasValue = false;
92-
}
93-
}
94-
95-
Optional &operator=(const Optional &Other) {
96-
if (HasValue && Other.HasValue) {
97-
Value = Other.Value;
98-
return *this;
99-
}
100-
101-
if (HasValue) {
102-
reset();
103-
return *this;
104-
}
105-
106-
if (Other.HasValue) {
107-
HasValue = true;
108-
::new ((void*)&Value) T(Other.Value);
109-
}
110-
111-
return *this;
112-
}
113-
114-
Optional &operator=(Optional &&Other) {
115-
if (HasValue && Other.HasValue) {
116-
Value = std::move(Other.Value);
117-
Other.reset();
118-
return *this;
119-
}
120-
121-
if (HasValue) {
122-
reset();
123-
return *this;
124-
}
125-
126-
if (Other.HasValue) {
127-
HasValue = true;
128-
::new ((void*)&Value) T(std::move(Other.Value));
129-
Other.reset();
130-
}
131-
132-
return *this;
133-
}
134-
135-
~Optional() { reset(); }
136-
137-
// Create a new object by constructing it in place with the given arguments.
138-
template<typename ...ArgTypes>
139-
void emplace(ArgTypes &&...Args) {
140-
reset();
141-
HasValue = true;
142-
::new ((void*)&Value) T(std::forward<ArgTypes>(Args)...);
143-
}
144-
145-
void reset() {
146-
if (!HasValue)
147-
return;
148-
149-
Value.~T();
150-
HasValue = false;
151-
}
152-
153-
T &getValue() & { assert(HasValue); return Value; }
154-
const T &getValue() const & { assert(HasValue); return Value; }
155-
156-
T getValue() && {
157-
assert(HasValue);
158-
T result = std::move(Value);
159-
reset();
160-
return result;
161-
}
162-
163-
template <typename U>
164-
constexpr T getValueOr(U &&value) const & {
165-
return hasValue() ? getValue() : value;
166-
}
167-
168-
template <typename U>
169-
T getValueOr(U &&value) && {
170-
return hasValue() ? getValue() : value;
171-
}
172-
173-
bool hasValue() const { return HasValue; }
174-
explicit operator bool() const { return HasValue; }
175-
176-
const T* operator->() const { assert(HasValue); return &Value; }
177-
T* operator->() { assert(HasValue); return &Value; }
178-
const T& operator*() const & { assert(HasValue); return Value; }
179-
T& operator*() & { assert(HasValue); return Value; }
180-
181-
T operator*() && {
182-
assert(HasValue);
183-
T result = std::move(Value);
184-
reset();
185-
return result;
186-
}
187-
188-
/// Return the value inside the optional if populated; otherwise, execute
189-
/// the given block, store its result inside the optional, and return it.
190-
template<typename NullaryFunctor>
191-
T const &cache(NullaryFunctor &&f) {
192-
if (hasValue())
193-
return getValue();
194-
emplace(f());
195-
return getValue();
196-
}
197-
};
33+
template <typename T>
34+
using Optional = llvm::Optional<T>;
19835
}
19936

20037
#endif

lib/IRGen/GenFunc.h

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
namespace swift {
2424
class ApplyInst;
2525
class FuncDecl;
26-
template <class T> class Optional;
2726
enum class ResilienceExpansion : unsigned;
2827
class Substitution;
2928
class SILType;

lib/IRGen/GenObjC.h

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef SWIFT_IRGEN_GENOBJC_H
1818
#define SWIFT_IRGEN_GENOBJC_H
1919

20+
#include "swift/Basic/Optional.h"
21+
2022
namespace llvm {
2123
class Type;
2224
class Value;

lib/IRGen/IRGenFunction.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define SWIFT_IRGEN_IRGENFUNCTION_H
2020

2121
#include "swift/Basic/LLVM.h"
22+
#include "swift/Basic/Optional.h"
2223
#include "swift/AST/Type.h"
2324
#include "swift/SIL/SILLocation.h"
2425
#include "llvm/ADT/DenseMap.h"
@@ -42,7 +43,6 @@ namespace swift {
4243
class FuncDecl;
4344
class EnumElementDecl;
4445
class EnumType;
45-
template<typename T> class Optional;
4646
class Pattern;
4747
class PatternBindingDecl;
4848
class SILDebugScope;

0 commit comments

Comments
 (0)