LLVM: include/llvm/ADT/StringSwitch.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13#ifndef LLVM_ADT_STRINGSWITCH_H
14#define LLVM_ADT_STRINGSWITCH_H
15
18#include
19#include
20#include <initializer_list>
21#include
22
23namespace llvm {
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46template<typename T, typename R = T>
48
50
51
52
53 std::optional Result;
54
55public:
57 : Str(S), Result() { }
58
60
61
63
64
67
68
70 CaseImpl(S, Value);
71 return *this;
72 }
73
75 if (!Result && Str.ends_with(S)) {
76 Result = std::move(Value);
77 }
78 return *this;
79 }
80
82 if (!Result && Str.starts_with(S)) {
83 Result = std::move(Value);
84 }
85 return *this;
86 }
87
90 return CasesImpl(CaseStrings, Value);
91 }
92
93 [[deprecated("Pass cases in std::initializer_list instead")]]
95 return CasesImpl({S0, S1}, Value);
96 }
97
98 [[deprecated("Pass cases in std::initializer_list instead")]]
101 return CasesImpl({S0, S1, S2}, Value);
102 }
103
104 [[deprecated("Pass cases in std::initializer_list instead")]]
107 return CasesImpl({S0, S1, S2, S3}, Value);
108 }
109
110 [[deprecated("Pass cases in std::initializer_list instead")]]
113 return CasesImpl({S0, S1, S2, S3, S4}, Value);
114 }
115
116 [[deprecated("Pass cases in std::initializer_list instead")]]
120 return CasesImpl({S0, S1, S2, S3, S4, S5}, Value);
121 }
122
123 [[deprecated("Pass cases in std::initializer_list instead")]]
127 return CasesImpl({S0, S1, S2, S3, S4, S5, S6}, Value);
128 }
129
130 [[deprecated("Pass cases in std::initializer_list instead")]]
134 return CasesImpl({S0, S1, S2, S3, S4, S5, S6, S7}, Value);
135 }
136
137 [[deprecated("Pass cases in std::initializer_list instead")]]
142 return CasesImpl({S0, S1, S2, S3, S4, S5, S6, S7, S8}, Value);
143 }
144
145 [[deprecated("Pass cases in std::initializer_list instead")]]
150 return CasesImpl({S0, S1, S2, S3, S4, S5, S6, S7, S8, S9}, Value);
151 }
152
153
155 CaseLowerImpl(S, Value);
156 return *this;
157 }
158
160 if (!Result && Str.ends_with_insensitive(S))
161 Result = std::move(Value);
162
163 return *this;
164 }
165
167 if (!Result && Str.starts_with_insensitive(S))
168 Result = std::move(Value);
169
170 return *this;
171 }
172
175 return CasesLowerImpl(CaseStrings, Value);
176 }
177
178 [[deprecated("Pass cases in std::initializer_list instead")]]
180 return CasesLowerImpl({S0, S1}, Value);
181 }
182
183 [[deprecated("Pass cases in std::initializer_list instead")]]
186 return CasesLowerImpl({S0, S1, S2}, Value);
187 }
188
189 [[deprecated("Pass cases in std::initializer_list instead")]]
192 return CasesLowerImpl({S0, S1, S2, S3}, Value);
193 }
194
195 [[deprecated("Pass cases in std::initializer_list instead")]]
198 return CasesLowerImpl({S0, S1, S2, S3, S4}, Value);
199 }
200
202 if (Result)
203 return std::move(*Result);
205 }
206
207
209 const char *Message = "Fell off the end of a string-switch") {
210 if (Result)
211 return std::move(*Result);
213 }
214
216
217private:
218
219
221 if (Result)
222 return true;
223
224 if (Str != S)
225 return false;
226
227 Result = std::move(Value);
228 return true;
229 }
230
231
232
234 if (Result)
235 return true;
236
237 if (!Str.equals_insensitive(S))
238 return false;
239
240 Result = std::move(Value);
241 return true;
242 }
243
244 StringSwitch &CasesImpl(std::initializer_list Cases,
246
247 for (StringLiteral S : Cases)
248 if (CaseImpl(S, Value))
249 break;
250 return *this;
251 }
252
253 StringSwitch &CasesLowerImpl(std::initializer_list Cases,
255
256 for (StringLiteral S : Cases)
257 if (CaseLowerImpl(S, Value))
258 break;
259 return *this;
260 }
261};
262
263}
264
265#endif
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
StringRef - Represent a constant reference to a string, i.e.
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, T Value)
Definition StringSwitch.h:196
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, T Value)
Definition StringSwitch.h:117
StringSwitch & EndsWithLower(StringLiteral S, T Value)
Definition StringSwitch.h:159
StringSwitch & StartsWithLower(StringLiteral S, T Value)
Definition StringSwitch.h:166
StringSwitch & CaseLower(StringLiteral S, T Value)
Definition StringSwitch.h:154
StringSwitch & Case(StringLiteral S, T Value)
Definition StringSwitch.h:69
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, T Value)
Definition StringSwitch.h:179
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, T Value)
Definition StringSwitch.h:111
void operator=(StringSwitch &&)=delete
void operator=(const StringSwitch &)=delete
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, T Value)
Definition StringSwitch.h:190
R DefaultUnreachable(const char *Message="Fell off the end of a string-switch")
Declare default as unreachable, making sure that all cases were handled.
Definition StringSwitch.h:208
R Default(T Value)
Definition StringSwitch.h:201
StringSwitch & CasesLower(std::initializer_list< StringLiteral > CaseStrings, T Value)
Definition StringSwitch.h:173
StringSwitch & StartsWith(StringLiteral S, T Value)
Definition StringSwitch.h:81
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, StringLiteral S7, StringLiteral S8, T Value)
Definition StringSwitch.h:138
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition StringSwitch.h:94
StringSwitch(const StringSwitch &)=delete
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, T Value)
Definition StringSwitch.h:105
StringSwitch & EndsWith(StringLiteral S, T Value)
Definition StringSwitch.h:74
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, StringLiteral S7, T Value)
Definition StringSwitch.h:131
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, T Value)
Definition StringSwitch.h:124
StringSwitch(StringSwitch &&)=default
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
Definition StringSwitch.h:99
StringSwitch & Cases(std::initializer_list< StringLiteral > CaseStrings, T Value)
Definition StringSwitch.h:88
StringSwitch(StringRef S)
Definition StringSwitch.h:56
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, StringLiteral S7, StringLiteral S8, StringLiteral S9, T Value)
Definition StringSwitch.h:146
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
Definition StringSwitch.h:184
LLVM Value Representation.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value