LLVM: include/llvm/ADT/fallible_iterator.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9#ifndef LLVM_ADT_FALLIBLE_ITERATOR_H
10#define LLVM_ADT_FALLIBLE_ITERATOR_H
11
15
16#include <type_traits>
17
18namespace llvm {
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68template class fallible_iterator {
69private:
70 template <typename T, typename U = decltype(std::declval().operator->())>
71 using enable_if_struct_deref_supported =
72 std::enable_if_t<!std::is_void_v, U>;
73
74public:
75
76
77
78
79
80
81
82
83 static fallible_iterator itr(Underlying I, Error &Err) {
84 (void)!!Err;
85 return fallible_iterator(std::move(I), &Err);
86 }
87
88
89
90
91
92
93 static fallible_iterator end(Underlying I) {
94 return fallible_iterator(std::move(I), nullptr);
95 }
96
97
98 decltype(auto) operator*() { return *I; }
99
100
101 decltype(auto) operator*() const { return *I; }
102
103
104
105 template
106 enable_if_struct_deref_supported operator->() {
107 return I.operator->();
108 }
109
110
111
112 template
113 enable_if_struct_deref_supported operator->() const {
114 return I.operator->();
115 }
116
117
118
119
120
121
122
123
125 assert(getErrPtr() && "Cannot increment end iterator");
126 if (auto Err = I.inc())
127 handleError(std::move(Err));
128 else
129 resetCheckedFlag();
130 return *this;
131 }
132
133
134
135
136
137
138
139
141 assert(getErrPtr() && "Cannot decrement end iterator");
142 if (auto Err = I.dec())
143 handleError(std::move(Err));
144 else
145 resetCheckedFlag();
146 return *this;
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
162 const fallible_iterator &RHS) {
163
164
165 if (LHS.isEnd() && RHS.isEnd())
166 return true;
167
169 "Invalid iterators can only be compared against end");
170
171 bool Equal = LHS.I == RHS.I;
172
173
174
175 if (!Equal) {
176 if (LHS.isEnd())
177 (void)!!*RHS.getErrPtr();
178 else
179 (void)!!*LHS.getErrPtr();
180 }
181
182 return Equal;
183 }
184
185
186
187
189 const fallible_iterator &RHS) {
191 }
192
193private:
196
197 Error *getErrPtr() const { return ErrState.getPointer(); }
198
199 bool isEnd() const { return getErrPtr() == nullptr; }
200
201 bool isValid() const { return !ErrState.getInt(); }
202
203 void handleError(Error Err) {
204 *getErrPtr() = std::move(Err);
205 ErrState.setPointer(nullptr);
206 ErrState.setInt(true);
207 }
208
209 void resetCheckedFlag() {
211 }
212
214 mutable PointerIntPair<Error *, 1> ErrState;
215};
216
217
218
219template
223
224
225
226template
230
231template
237
238}
239
240#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the PointerIntPair class.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
A wrapper class for fallible iterators.
Definition fallible_iterator.h:68
static fallible_iterator end(Underlying I)
Construct a fallible iterator that can be used as an end-of-range value.
Definition fallible_iterator.h:93
friend bool operator!=(const fallible_iterator &LHS, const fallible_iterator &RHS)
Compare fallible iterators for inequality.
Definition fallible_iterator.h:188
static fallible_iterator itr(Underlying I, Error &Err)
Construct a fallible iterator that cannot be used as an end-of-range value.
Definition fallible_iterator.h:83
fallible_iterator & operator--()
Decrement the fallible iterator.
Definition fallible_iterator.h:140
friend bool operator==(const fallible_iterator &LHS, const fallible_iterator &RHS)
Compare fallible iterators for equality.
Definition fallible_iterator.h:161
fallible_iterator & operator++()
Increment the fallible iterator.
Definition fallible_iterator.h:124
enable_if_struct_deref_supported< const T > operator->() const
Forward const structure dereference to the underlying iterator (if the underlying iterator supports i...
Definition fallible_iterator.h:113
enable_if_struct_deref_supported< T > operator->()
Forward structure dereference to the underlying iterator (if the underlying iterator supports it).
Definition fallible_iterator.h:106
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
constexpr std::underlying_type_t< E > Underlying(E Val)
Check that Val is in range for E, and return Val cast to E's underlying type.
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
fallible_iterator< Underlying > make_fallible_itr(Underlying I, Error &Err)
Convenience wrapper to make a fallible_iterator value from an instance of an underlying iterator and ...
Definition fallible_iterator.h:220
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
fallible_iterator< Underlying > make_fallible_end(Underlying E)
Convenience wrapper to make a fallible_iterator end value from an instance of an underlying iterator.
Definition fallible_iterator.h:227
iterator_range< fallible_iterator< Underlying > > make_fallible_range(Underlying I, Underlying E, Error &Err)
Definition fallible_iterator.h:233
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.