LLVM: include/llvm/ADT/StringSwitch.h Source File

LLVM 22.0.0git
StringSwitch.h
Go to the documentation of this file.
1//===--- StringSwitch.h - Switch-on-literal-string Construct --------------===/
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/// \file
9/// This file implements the StringSwitch template, which mimics a switch()
10/// statement whose cases are string literals.
11///
12//===----------------------------------------------------------------------===/
13#ifndef LLVM_ADT_STRINGSWITCH_H
14#define LLVM_ADT_STRINGSWITCH_H
15
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/ErrorHandling.h"
19#include <cassert>
20#include <cstring>
21#include <initializer_list>
22#include <optional>
23
24namespace llvm {
25
26/// A switch()-like statement whose cases are string literals.
27///
28/// The StringSwitch class is a simple form of a switch() statement that
29/// determines whether the given string matches one of the given string
30/// literals. The template type parameter \p T is the type of the value that
31/// will be returned from the string-switch expression. For example,
32/// the following code switches on the name of a color in \c argv[i]:
33///
34/// \code
35/// Color color = StringSwitch<Color>(argv[i])
36/// .Case("red", Red)
37/// .Case("orange", Orange)
38/// .Case("yellow", Yellow)
39/// .Case("green", Green)
40/// .Case("blue", Blue)
41/// .Case("indigo", Indigo)
42/// .Cases({"violet", "purple"}, Violet)
43/// .Default(UnknownColor);
44/// \endcode
45template<typename T, typename R = T>
47 /// The string we are matching.
48 const StringRef Str;
49
50 /// The pointer to the result of this switch statement, once known,
51 /// null before that.
52 std::optional<T> Result;
53
54public:
56 : Str(S), Result() { }
57
59
60 // StringSwitch is not copyable.
61 StringSwitch(const StringSwitch &) = delete;
62
63 // StringSwitch is not assignable due to 'Str' being 'const'.
64 void operator=(const StringSwitch &) = delete;
65 void operator=(StringSwitch &&) = delete;
66
67 // Case-sensitive case matchers
69 CaseImpl(S, Value);
70 return *this;
71 }
72
74 if (!Result && Str.ends_with(S)) {
75 Result = std::move(Value);
76 }
77 return *this;
78 }
79
81 if (!Result && Str.starts_with(S)) {
82 Result = std::move(Value);
83 }
84 return *this;
85 }
86
87 StringSwitch &Cases(std::initializer_list<StringLiteral> CaseStrings,
88 T Value) {
89 return CasesImpl(CaseStrings, Value);
90 }
91
93 return CasesImpl({S0, S1}, Value);
94 }
95
96 [[deprecated("Pass cases in std::initializer_list instead")]]
98 T Value) {
99 return CasesImpl({S0, S1, S2}, Value);
100 }
101
102 [[deprecated("Pass cases in std::initializer_list instead")]]
104 StringLiteral S3, T Value) {
105 return CasesImpl({S0, S1, S2, S3}, Value);
106 }
107
108 [[deprecated("Pass cases in std::initializer_list instead")]]
111 return CasesImpl({S0, S1, S2, S3, S4}, Value);
112 }
113
114 [[deprecated("Pass cases in std::initializer_list instead")]]
117 T Value) {
118 return CasesImpl({S0, S1, S2, S3, S4, S5}, Value);
119 }
120
121 [[deprecated("Pass cases in std::initializer_list instead")]]
124 StringLiteral S6, T Value) {
125 return CasesImpl({S0, S1, S2, S3, S4, S5, S6}, Value);
126 }
127
128 [[deprecated("Pass cases in std::initializer_list instead")]]
132 return CasesImpl({S0, S1, S2, S3, S4, S5, S6, S7}, Value);
133 }
134
135 [[deprecated("Pass cases in std::initializer_list instead")]]
139 T Value) {
140 return CasesImpl({S0, S1, S2, S3, S4, S5, S6, S7, S8}, Value);
141 }
142
143 [[deprecated("Pass cases in std::initializer_list instead")]]
147 StringLiteral S9, T Value) {
148 return CasesImpl({S0, S1, S2, S3, S4, S5, S6, S7, S8, S9}, Value);
149 }
150
151 // Case-insensitive case matchers.
153 CaseLowerImpl(S, Value);
154 return *this;
155 }
156
158 if (!Result && Str.ends_with_insensitive(S))
159 Result = Value;
160
161 return *this;
162 }
163
165 if (!Result && Str.starts_with_insensitive(S))
166 Result = std::move(Value);
167
168 return *this;
169 }
170
171 StringSwitch &CasesLower(std::initializer_list<StringLiteral> CaseStrings,
172 T Value) {
173 return CasesLowerImpl(CaseStrings, Value);
174 }
175
177 return CasesLowerImpl({S0, S1}, Value);
178 }
179
180 [[deprecated("Pass cases in std::initializer_list instead")]]
182 T Value) {
183 return CasesLowerImpl({S0, S1, S2}, Value);
184 }
185
186 [[deprecated("Pass cases in std::initializer_list instead")]]
188 StringLiteral S3, T Value) {
189 return CasesLowerImpl({S0, S1, S2, S3}, Value);
190 }
191
192 [[deprecated("Pass cases in std::initializer_list instead")]]
195 return CasesLowerImpl({S0, S1, S2, S3, S4}, Value);
196 }
197
198 [[nodiscard]] R Default(T Value) {
199 if (Result)
200 return std::move(*Result);
201 return Value;
202 }
203
204 /// Declare default as unreachable, making sure that all cases were handled.
205 [[nodiscard]] R DefaultUnreachable(
206 const char *Message = "Fell off the end of a string-switch") {
207 if (Result)
208 return std::move(*Result);
209 llvm_unreachable(Message);
210 }
211
212 [[nodiscard]] operator R() { return DefaultUnreachable(); }
213
214private:
215 // Returns true when `Str` matches the `S` argument, and stores the result.
216 bool CaseImpl(StringLiteral S, T &Value) {
217 if (!Result && Str == S) {
218 Result = std::move(Value);
219 return true;
220 }
221 return false;
222 }
223
224 // Returns true when `Str` matches the `S` argument (case-insensitive), and
225 // stores the result.
226 bool CaseLowerImpl(StringLiteral S, T &Value) {
227 if (!Result && Str.equals_insensitive(S)) {
228 Result = std::move(Value);
229 return true;
230 }
231 return false;
232 }
233
234 StringSwitch &CasesImpl(std::initializer_list<StringLiteral> Cases,
235 T &Value) {
236 // Stop matching after the string is found.
237 for (StringLiteral S : Cases)
238 if (CaseImpl(S, Value))
239 break;
240 return *this;
241 }
242
243 StringSwitch &CasesLowerImpl(std::initializer_list<StringLiteral> Cases,
244 T &Value) {
245 // Stop matching after the string is found.
246 for (StringLiteral S : Cases)
247 if (CaseLowerImpl(S, Value))
248 break;
249 return *this;
250 }
251};
252
253} // end namespace llvm
254
255#endif // LLVM_ADT_STRINGSWITCH_H
constexpr LLT S1
constexpr LLT S8
T
#define T
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:854
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, T Value)
Definition StringSwitch.h:193
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, T Value)
Definition StringSwitch.h:115
StringSwitch & EndsWithLower(StringLiteral S, T Value)
Definition StringSwitch.h:157
StringSwitch & StartsWithLower(StringLiteral S, T Value)
Definition StringSwitch.h:164
StringSwitch & CaseLower(StringLiteral S, T Value)
Definition StringSwitch.h:152
StringSwitch & Case(StringLiteral S, T Value)
Definition StringSwitch.h:68
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, T Value)
Definition StringSwitch.h:176
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, T Value)
Definition StringSwitch.h:109
void operator=(StringSwitch &&)=delete
void operator=(const StringSwitch &)=delete
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, T Value)
Definition StringSwitch.h:187
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:205
R Default(T Value)
Definition StringSwitch.h:198
StringSwitch & CasesLower(std::initializer_list< StringLiteral > CaseStrings, T Value)
Definition StringSwitch.h:171
StringSwitch & StartsWith(StringLiteral S, T Value)
Definition StringSwitch.h:80
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:136
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition StringSwitch.h:92
StringSwitch(const StringSwitch &)=delete
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, T Value)
Definition StringSwitch.h:103
StringSwitch & EndsWith(StringLiteral S, T Value)
Definition StringSwitch.h:73
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, StringLiteral S7, T Value)
Definition StringSwitch.h:129
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, T Value)
Definition StringSwitch.h:122
StringSwitch(StringSwitch &&)=default
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
Definition StringSwitch.h:97
StringSwitch & Cases(std::initializer_list< StringLiteral > CaseStrings, T Value)
Definition StringSwitch.h:87
StringSwitch(StringRef S)
Definition StringSwitch.h:55
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:144
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
Definition StringSwitch.h:181
LLVM Value Representation.
Definition Value.h:75
#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.
Definition AddressRanges.h:18
FunctionAddr VTableAddr Value
Definition InstrProf.h:137

Generated on for LLVM by doxygen 1.14.0

AltStyle によって変換されたページ (->オリジナル) /