LLVM  14.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 // This file implements the StringSwitch template, which mimics a switch()
9 // statement whose cases are string literals.
10 //
11 //===----------------------------------------------------------------------===/
12 #ifndef LLVM_ADT_STRINGSWITCH_H
13 #define LLVM_ADT_STRINGSWITCH_H
14 
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Compiler.h"
17 #include <cassert>
18 #include <cstring>
19 
20 namespace llvm {
21 
22 /// A switch()-like statement whose cases are string literals.
23 ///
24 /// The StringSwitch class is a simple form of a switch() statement that
25 /// determines whether the given string matches one of the given string
26 /// literals. The template type parameter \p T is the type of the value that
27 /// will be returned from the string-switch expression. For example,
28 /// the following code switches on the name of a color in \c argv[i]:
29 ///
30 /// \code
31 /// Color color = StringSwitch<Color>(argv[i])
32 /// .Case("red", Red)
33 /// .Case("orange", Orange)
34 /// .Case("yellow", Yellow)
35 /// .Case("green", Green)
36 /// .Case("blue", Blue)
37 /// .Case("indigo", Indigo)
38 /// .Cases("violet", "purple", Violet)
39 /// .Default(UnknownColor);
40 /// \endcode
41 template<typename T, typename R = T>
42 class StringSwitch {
43  /// The string we are matching.
44  const StringRef Str;
45 
46  /// The pointer to the result of this switch statement, once known,
47  /// null before that.
48  Optional<T> Result;
49 
50 public:
52  : Str(S), Result() { }
53 
54  // StringSwitch is not copyable.
55  StringSwitch(const StringSwitch &) = delete;
56 
57  // StringSwitch is not assignable due to 'Str' being 'const'.
58  void operator=(const StringSwitch &) = delete;
59  void operator=(StringSwitch &&other) = delete;
60 
62  : Str(other.Str), Result(std::move(other.Result)) { }
63 
64  ~StringSwitch() = default;
65 
66  // Case-sensitive case matchers
68  if (!Result && Str == S) {
69  Result = std::move(Value);
70  }
71  return *this;
72  }
73 
75  if (!Result && Str.endswith(S)) {
76  Result = std::move(Value);
77  }
78  return *this;
79  }
80 
82  if (!Result && Str.startswith(S)) {
83  Result = std::move(Value);
84  }
85  return *this;
86  }
87 
89  return Case(S0, Value).Case(S1, Value);
90  }
91 
93  T Value) {
94  return Case(S0, Value).Cases(S1, S2, Value);
95  }
96 
98  StringLiteral S3, T Value) {
99  return Case(S0, Value).Cases(S1, S2, S3, Value);
100  }
101 
103  StringLiteral S3, StringLiteral S4, T Value) {
104  return Case(S0, Value).Cases(S1, S2, S3, S4, Value);
105  }
106 
109  T Value) {
110  return Case(S0, Value).Cases(S1, S2, S3, S4, S5, Value);
111  }
112 
115  StringLiteral S6, T Value) {
116  return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, Value);
117  }
118 
121  StringLiteral S6, StringLiteral S7, T Value) {
122  return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, Value);
123  }
124 
128  T Value) {
129  return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, Value);
130  }
131 
135  StringLiteral S9, T Value) {
136  return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, S9, Value);
137  }
138 
139  // Case-insensitive case matchers.
141  if (!Result && Str.equals_insensitive(S))
142  Result = std::move(Value);
143 
144  return *this;
145  }
146 
148  if (!Result && Str.endswith_insensitive(S))
149  Result = Value;
150 
151  return *this;
152  }
153 
155  if (!Result && Str.startswith_insensitive(S))
156  Result = std::move(Value);
157 
158  return *this;
159  }
160 
162  return CaseLower(S0, Value).CaseLower(S1, Value);
163  }
164 
166  T Value) {
167  return CaseLower(S0, Value).CasesLower(S1, S2, Value);
168  }
169 
171  StringLiteral S3, T Value) {
172  return CaseLower(S0, Value).CasesLower(S1, S2, S3, Value);
173  }
174 
176  StringLiteral S3, StringLiteral S4, T Value) {
177  return CaseLower(S0, Value).CasesLower(S1, S2, S3, S4, Value);
178  }
179 
181  R Default(T Value) {
182  if (Result)
183  return std::move(*Result);
184  return Value;
185  }
186 
188  operator R() {
189  assert(Result && "Fell off the end of a string-switch");
190  return std::move(*Result);
191  }
192 };
193 
194 } // end namespace llvm
195 
196 #endif // LLVM_ADT_STRINGSWITCH_H
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, T Value)
Definition: StringSwitch.h:107
llvm::StringSwitch::StartsWith
StringSwitch & StartsWith(StringLiteral S, T Value)
Definition: StringSwitch.h:81
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::StringSwitch::Cases
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:125
StringRef.h
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, StringLiteral S7, T Value)
Definition: StringSwitch.h:119
llvm::StringSwitch::EndsWith
StringSwitch & EndsWith(StringLiteral S, T Value)
Definition: StringSwitch.h:74
llvm::Optional
Definition: APInt.h:33
llvm::StringSwitch::CaseLower
StringSwitch & CaseLower(StringLiteral S, T Value)
Definition: StringSwitch.h:140
llvm::StringSwitch::StartsWithLower
StringSwitch & StartsWithLower(StringLiteral S, T Value)
Definition: StringSwitch.h:154
llvm::StringLiteral
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:891
llvm::StringSwitch::EndsWithLower
StringSwitch & EndsWithLower(StringLiteral S, T Value)
Definition: StringSwitch.h:147
llvm::StringSwitch::Cases
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:132
llvm::StringSwitch::StringSwitch
StringSwitch(StringSwitch &&other)
Definition: StringSwitch.h:61
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, T Value)
Definition: StringSwitch.h:97
llvm::StringSwitch::CasesLower
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:161
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, T Value)
Definition: StringSwitch.h:113
llvm::StringSwitch::CasesLower
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, T Value)
Definition: StringSwitch.h:175
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, T Value)
Definition: StringSwitch.h:102
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1605
llvm::StringSwitch::operator=
void operator=(const StringSwitch &)=delete
llvm::StringSwitch::StringSwitch
StringSwitch(StringRef S)
Definition: StringSwitch.h:51
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
Compiler.h
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:88
std
Definition: BitVector.h:838
LLVM_NODISCARD
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
Definition: Compiler.h:161
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::StringSwitch::~StringSwitch
~StringSwitch()=default
llvm::StringSwitch::CasesLower
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, T Value)
Definition: StringSwitch.h:170
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
llvm::StringSwitch::CasesLower
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
Definition: StringSwitch.h:165
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
Definition: StringSwitch.h:92
llvm::Value
LLVM Value Representation.
Definition: Value.h:75