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"
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
92 [[deprecated("Pass cases in std::initializer_list instead")]]
94 return CasesImpl({S0, S1}, Value);
95 }
96
97 [[deprecated("Pass cases in std::initializer_list instead")]]
99 T Value) {
100 return CasesImpl({S0, S1, S2}, Value);
101 }
102
103 [[deprecated("Pass cases in std::initializer_list instead")]]
105 StringLiteral S3, T Value) {
106 return CasesImpl({S0, S1, S2, S3}, Value);
107 }
108
109 [[deprecated("Pass cases in std::initializer_list instead")]]
112 return CasesImpl({S0, S1, S2, S3, S4}, Value);
113 }
114
115 [[deprecated("Pass cases in std::initializer_list instead")]]
118 T Value) {
119 return CasesImpl({S0, S1, S2, S3, S4, S5}, Value);
120 }
121
122 [[deprecated("Pass cases in std::initializer_list instead")]]
125 StringLiteral S6, T Value) {
126 return CasesImpl({S0, S1, S2, S3, S4, S5, S6}, Value);
127 }
128
129 [[deprecated("Pass cases in std::initializer_list instead")]]
133 return CasesImpl({S0, S1, S2, S3, S4, S5, S6, S7}, Value);
134 }
135
136 [[deprecated("Pass cases in std::initializer_list instead")]]
140 T Value) {
141 return CasesImpl({S0, S1, S2, S3, S4, S5, S6, S7, S8}, Value);
142 }
143
144 [[deprecated("Pass cases in std::initializer_list instead")]]
148 StringLiteral S9, T Value) {
149 return CasesImpl({S0, S1, S2, S3, S4, S5, S6, S7, S8, S9}, Value);
150 }
151
152 // Case-insensitive case matchers.
154 CaseLowerImpl(S, Value);
155 return *this;
156 }
157
159 if (!Result && Str.ends_with_insensitive(S))
160 Result = Value;
161
162 return *this;
163 }
164
166 if (!Result && Str.starts_with_insensitive(S))
167 Result = std::move(Value);
168
169 return *this;
170 }
171
172 StringSwitch &CasesLower(std::initializer_list<StringLiteral> CaseStrings,
173 T Value) {
174 return CasesLowerImpl(CaseStrings, Value);
175 }
176
177 [[deprecated("Pass cases in std::initializer_list instead")]]
179 return CasesLowerImpl({S0, S1}, Value);
180 }
181
182 [[deprecated("Pass cases in std::initializer_list instead")]]
184 T Value) {
185 return CasesLowerImpl({S0, S1, S2}, Value);
186 }
187
188 [[deprecated("Pass cases in std::initializer_list instead")]]
190 StringLiteral S3, T Value) {
191 return CasesLowerImpl({S0, S1, S2, S3}, Value);
192 }
193
194 [[deprecated("Pass cases in std::initializer_list instead")]]
197 return CasesLowerImpl({S0, S1, S2, S3, S4}, Value);
198 }
199
200 [[nodiscard]] R Default(T Value) {
201 if (Result)
202 return std::move(*Result);
203 return Value;
204 }
205
206 /// Declare default as unreachable, making sure that all cases were handled.
207 [[nodiscard]] R DefaultUnreachable(
208 const char *Message = "Fell off the end of a string-switch") {
209 if (Result)
210 return std::move(*Result);
211 llvm_unreachable(Message);
212 }
213
214 [[nodiscard]] operator R() { return DefaultUnreachable(); }
215
216private:
217 // Returns true when `Str` matches the `S` argument, and stores the result.
218 bool CaseImpl(StringLiteral S, T &Value) {
219 if (!Result && Str == S) {
220 Result = std::move(Value);
221 return true;
222 }
223 return false;
224 }
225
226 // Returns true when `Str` matches the `S` argument (case-insensitive), and
227 // stores the result.
228 bool CaseLowerImpl(StringLiteral S, T &Value) {
229 if (!Result && Str.equals_insensitive(S)) {
230 Result = std::move(Value);
231 return true;
232 }
233 return false;
234 }
235
236 StringSwitch &CasesImpl(std::initializer_list<StringLiteral> Cases,
237 T &Value) {
238 // Stop matching after the string is found.
239 for (StringLiteral S : Cases)
240 if (CaseImpl(S, Value))
241 break;
242 return *this;
243 }
244
245 StringSwitch &CasesLowerImpl(std::initializer_list<StringLiteral> Cases,
246 T &Value) {
247 // Stop matching after the string is found.
248 for (StringLiteral S : Cases)
249 if (CaseLowerImpl(S, Value))
250 break;
251 return *this;
252 }
253};
254
255} // end namespace llvm
256
257#endif // LLVM_ADT_STRINGSWITCH_H
constexpr LLT S1
constexpr LLT S8
#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)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, T Value)
StringSwitch & EndsWithLower(StringLiteral S, T Value)
StringSwitch & StartsWithLower(StringLiteral S, T Value)
StringSwitch & CaseLower(StringLiteral S, T Value)
StringSwitch & Case(StringLiteral S, T Value)
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, T Value)
void operator=(StringSwitch &&)=delete
void operator=(const StringSwitch &)=delete
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, T Value)
R DefaultUnreachable(const char *Message="Fell off the end of a string-switch")
Declare default as unreachable, making sure that all cases were handled.
StringSwitch & CasesLower(std::initializer_list< StringLiteral > CaseStrings, T Value)
StringSwitch & StartsWith(StringLiteral S, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, StringLiteral S7, StringLiteral S8, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
StringSwitch(const StringSwitch &)=delete
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, T Value)
StringSwitch & EndsWith(StringLiteral S, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, StringLiteral S7, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, T Value)
StringSwitch(StringSwitch &&)=default
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
StringSwitch & Cases(std::initializer_list< StringLiteral > CaseStrings, T Value)
StringSwitch(StringRef S)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, StringLiteral S7, StringLiteral S8, StringLiteral S9, T Value)
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
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.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137