LLVM  16.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/Optional.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Compiler.h"
19 #include <cassert>
20 #include <cstring>
21 
22 namespace llvm {
23 
24 /// A switch()-like statement whose cases are string literals.
25 ///
26 /// The StringSwitch class is a simple form of a switch() statement that
27 /// determines whether the given string matches one of the given string
28 /// literals. The template type parameter \p T is the type of the value that
29 /// will be returned from the string-switch expression. For example,
30 /// the following code switches on the name of a color in \c argv[i]:
31 ///
32 /// \code
33 /// Color color = StringSwitch<Color>(argv[i])
34 /// .Case("red", Red)
35 /// .Case("orange", Orange)
36 /// .Case("yellow", Yellow)
37 /// .Case("green", Green)
38 /// .Case("blue", Blue)
39 /// .Case("indigo", Indigo)
40 /// .Cases("violet", "purple", Violet)
41 /// .Default(UnknownColor);
42 /// \endcode
43 template<typename T, typename R = T>
44 class StringSwitch {
45  /// The string we are matching.
46  const StringRef Str;
47 
48  /// The pointer to the result of this switch statement, once known,
49  /// null before that.
50  Optional<T> Result;
51 
52 public:
54  : Str(S), Result() { }
55 
56  // StringSwitch is not copyable.
57  StringSwitch(const StringSwitch &) = delete;
58 
59  // StringSwitch is not assignable due to 'Str' being 'const'.
60  void operator=(const StringSwitch &) = delete;
61  void operator=(StringSwitch &&other) = delete;
62 
64  : Str(other.Str), Result(std::move(other.Result)) { }
65 
66  ~StringSwitch() = default;
67 
68  // Case-sensitive case matchers
70  if (!Result && Str == S) {
71  Result = std::move(Value);
72  }
73  return *this;
74  }
75 
77  if (!Result && Str.endswith(S)) {
78  Result = std::move(Value);
79  }
80  return *this;
81  }
82 
84  if (!Result && Str.startswith(S)) {
85  Result = std::move(Value);
86  }
87  return *this;
88  }
89 
91  return Case(S0, Value).Case(S1, Value);
92  }
93 
95  T Value) {
96  return Case(S0, Value).Cases(S1, S2, Value);
97  }
98 
100  StringLiteral S3, T Value) {
101  return Case(S0, Value).Cases(S1, S2, S3, Value);
102  }
103 
105  StringLiteral S3, StringLiteral S4, T Value) {
106  return Case(S0, Value).Cases(S1, S2, S3, S4, Value);
107  }
108 
111  T Value) {
112  return Case(S0, Value).Cases(S1, S2, S3, S4, S5, Value);
113  }
114 
117  StringLiteral S6, T Value) {
118  return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, Value);
119  }
120 
123  StringLiteral S6, StringLiteral S7, T Value) {
124  return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, Value);
125  }
126 
130  T Value) {
131  return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, Value);
132  }
133 
137  StringLiteral S9, T Value) {
138  return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, S9, Value);
139  }
140 
141  // Case-insensitive case matchers.
143  if (!Result && Str.equals_insensitive(S))
144  Result = std::move(Value);
145 
146  return *this;
147  }
148 
150  if (!Result && Str.endswith_insensitive(S))
151  Result = Value;
152 
153  return *this;
154  }
155 
157  if (!Result && Str.startswith_insensitive(S))
158  Result = std::move(Value);
159 
160  return *this;
161  }
162 
164  return CaseLower(S0, Value).CaseLower(S1, Value);
165  }
166 
168  T Value) {
169  return CaseLower(S0, Value).CasesLower(S1, S2, Value);
170  }
171 
173  StringLiteral S3, T Value) {
174  return CaseLower(S0, Value).CasesLower(S1, S2, S3, Value);
175  }
176 
178  StringLiteral S3, StringLiteral S4, T Value) {
179  return CaseLower(S0, Value).CasesLower(S1, S2, S3, S4, Value);
180  }
181 
182  [[nodiscard]] R Default(T Value) {
183  if (Result)
184  return std::move(*Result);
185  return Value;
186  }
187 
188  [[nodiscard]] 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:69
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, T Value)
Definition: StringSwitch.h:109
llvm::StringSwitch::StartsWith
StringSwitch & StartsWith(StringLiteral S, T Value)
Definition: StringSwitch.h:83
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Optional.h
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:127
StringRef.h
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:121
llvm::StringSwitch::EndsWith
StringSwitch & EndsWith(StringLiteral S, T Value)
Definition: StringSwitch.h:76
llvm::Optional
Definition: APInt.h:33
llvm::StringSwitch::CaseLower
StringSwitch & CaseLower(StringLiteral S, T Value)
Definition: StringSwitch.h:142
llvm::StringSwitch::StartsWithLower
StringSwitch & StartsWithLower(StringLiteral S, T Value)
Definition: StringSwitch.h:156
llvm::StringLiteral
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:842
llvm::StringSwitch::EndsWithLower
StringSwitch & EndsWithLower(StringLiteral S, T Value)
Definition: StringSwitch.h:149
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:134
llvm::StringSwitch::StringSwitch
StringSwitch(StringSwitch &&other)
Definition: StringSwitch.h:63
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, T Value)
Definition: StringSwitch.h:99
llvm::StringSwitch::CasesLower
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:163
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, T Value)
Definition: StringSwitch.h:115
llvm::StringSwitch::CasesLower
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, T Value)
Definition: StringSwitch.h:177
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, T Value)
Definition: StringSwitch.h:104
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:1666
llvm::StringSwitch::operator=
void operator=(const StringSwitch &)=delete
llvm::StringSwitch::StringSwitch
StringSwitch(StringRef S)
Definition: StringSwitch.h:53
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:90
std
Definition: BitVector.h:851
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:172
llvm::StringSwitch::Default
R Default(T Value)
Definition: StringSwitch.h:182
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
llvm::StringSwitch::CasesLower
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
Definition: StringSwitch.h:167
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
Definition: StringSwitch.h:94
llvm::Value
LLVM Value Representation.
Definition: Value.h:74