Skip to content

Commit 9fe01d7

Browse files
committed
Merge remote-tracking branch 'origin/master' into master-next
2 parents caab5ec + 9bde41a commit 9fe01d7

File tree

7 files changed

+225
-27
lines changed

7 files changed

+225
-27
lines changed

include/swift/Basic/LLVM.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ namespace llvm {
3636
template <typename T, unsigned N> class SmallPtrSet;
3737
#if !defined(swiftCore_EXPORTS)
3838
template <typename T> class SmallVectorImpl;
39-
#endif
4039
template <typename T, unsigned N> class SmallVector;
40+
#endif
4141
template <unsigned N> class SmallString;
4242
template <typename T, unsigned N> class SmallSetVector;
4343
#if !defined(swiftCore_EXPORTS)
@@ -86,8 +86,8 @@ namespace swift {
8686
using llvm::SmallPtrSetImpl;
8787
using llvm::SmallSetVector;
8888
using llvm::SmallString;
89-
using llvm::SmallVector;
9089
#if !defined(swiftCore_EXPORTS)
90+
using llvm::SmallVector;
9191
using llvm::SmallVectorImpl;
9292
#endif
9393
using llvm::StringLiteral;

include/swift/Demangling/TypeDecoder.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ class TypeDecoder {
361361
if (Node->getNumChildren() < 2)
362362
return BuiltType();
363363

364-
SmallVector<BuiltType, 8> args;
364+
llvm::SmallVector<BuiltType, 8> args;
365365

366366
const auto &genericArgs = Node->getChild(1);
367367
if (genericArgs->getKind() != NodeKind::TypeList)
@@ -474,7 +474,7 @@ class TypeDecoder {
474474
return BuiltType();
475475

476476
// Find the protocol list.
477-
SmallVector<BuiltProtocolDecl, 8> Protocols;
477+
llvm::SmallVector<BuiltProtocolDecl, 8> Protocols;
478478
auto TypeList = Node->getChild(0);
479479
if (TypeList->getKind() == NodeKind::ProtocolList &&
480480
TypeList->getNumChildren() >= 1) {
@@ -576,7 +576,7 @@ class TypeDecoder {
576576
return BuiltType();
577577

578578
bool hasParamFlags = false;
579-
SmallVector<FunctionParam<BuiltType>, 8> parameters;
579+
llvm::SmallVector<FunctionParam<BuiltType>, 8> parameters;
580580
if (!decodeMangledFunctionInputType(Node->getChild(isThrow ? 1 : 0),
581581
parameters, hasParamFlags))
582582
return BuiltType();
@@ -598,9 +598,9 @@ class TypeDecoder {
598598
}
599599
case NodeKind::ImplFunctionType: {
600600
auto calleeConvention = ImplParameterConvention::Direct_Unowned;
601-
SmallVector<ImplFunctionParam<BuiltType>, 8> parameters;
602-
SmallVector<ImplFunctionResult<BuiltType>, 8> results;
603-
SmallVector<ImplFunctionResult<BuiltType>, 8> errorResults;
601+
llvm::SmallVector<ImplFunctionParam<BuiltType>, 8> parameters;
602+
llvm::SmallVector<ImplFunctionResult<BuiltType>, 8> results;
603+
llvm::SmallVector<ImplFunctionResult<BuiltType>, 8> errorResults;
604604
ImplFunctionTypeFlags flags;
605605

606606
for (unsigned i = 0; i < Node->getNumChildren(); i++) {
@@ -684,7 +684,7 @@ class TypeDecoder {
684684
return decodeMangledType(Node->getChild(0));
685685

686686
case NodeKind::Tuple: {
687-
SmallVector<BuiltType, 8> elements;
687+
llvm::SmallVector<BuiltType, 8> elements;
688688
std::string labels;
689689
bool variadic = false;
690690
for (auto &element : *Node) {
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
inline namespace __swift { inline namespace __runtime {
21+
namespace llvm {
22+
23+
/// A switch()-like statement whose cases are string literals.
24+
///
25+
/// The StringSwitch class is a simple form of a switch() statement that
26+
/// determines whether the given string matches one of the given string
27+
/// literals. The template type parameter \p T is the type of the value that
28+
/// will be returned from the string-switch expression. For example,
29+
/// the following code switches on the name of a color in \c argv[i]:
30+
///
31+
/// \code
32+
/// Color color = StringSwitch<Color>(argv[i])
33+
/// .Case("red", Red)
34+
/// .Case("orange", Orange)
35+
/// .Case("yellow", Yellow)
36+
/// .Case("green", Green)
37+
/// .Case("blue", Blue)
38+
/// .Case("indigo", Indigo)
39+
/// .Cases("violet", "purple", Violet)
40+
/// .Default(UnknownColor);
41+
/// \endcode
42+
template<typename T, typename R = T>
43+
class StringSwitch {
44+
/// The string we are matching.
45+
const StringRef Str;
46+
47+
/// The pointer to the result of this switch statement, once known,
48+
/// null before that.
49+
Optional<T> Result;
50+
51+
public:
52+
explicit StringSwitch(StringRef S)
53+
: Str(S), Result() { }
54+
55+
// StringSwitch is not copyable.
56+
StringSwitch(const StringSwitch &) = delete;
57+
58+
// StringSwitch is not assignable due to 'Str' being 'const'.
59+
void operator=(const StringSwitch &) = delete;
60+
void operator=(StringSwitch &&other) = delete;
61+
62+
StringSwitch(StringSwitch &&other)
63+
: Str(other.Str), Result(std::move(other.Result)) { }
64+
65+
~StringSwitch() = default;
66+
67+
// Case-sensitive case matchers
68+
StringSwitch &Case(StringLiteral S, T Value) {
69+
if (!Result && Str == S) {
70+
Result = std::move(Value);
71+
}
72+
return *this;
73+
}
74+
75+
StringSwitch& EndsWith(StringLiteral S, T Value) {
76+
if (!Result && Str.endswith(S)) {
77+
Result = std::move(Value);
78+
}
79+
return *this;
80+
}
81+
82+
StringSwitch& StartsWith(StringLiteral S, T Value) {
83+
if (!Result && Str.startswith(S)) {
84+
Result = std::move(Value);
85+
}
86+
return *this;
87+
}
88+
89+
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, T Value) {
90+
return Case(S0, Value).Case(S1, Value);
91+
}
92+
93+
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
94+
T Value) {
95+
return Case(S0, Value).Cases(S1, S2, Value);
96+
}
97+
98+
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
99+
StringLiteral S3, T Value) {
100+
return Case(S0, Value).Cases(S1, S2, S3, Value);
101+
}
102+
103+
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
104+
StringLiteral S3, StringLiteral S4, T Value) {
105+
return Case(S0, Value).Cases(S1, S2, S3, S4, Value);
106+
}
107+
108+
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
109+
StringLiteral S3, StringLiteral S4, StringLiteral S5,
110+
T Value) {
111+
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, Value);
112+
}
113+
114+
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
115+
StringLiteral S3, StringLiteral S4, StringLiteral S5,
116+
StringLiteral S6, T Value) {
117+
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, Value);
118+
}
119+
120+
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
121+
StringLiteral S3, StringLiteral S4, StringLiteral S5,
122+
StringLiteral S6, StringLiteral S7, T Value) {
123+
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, Value);
124+
}
125+
126+
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
127+
StringLiteral S3, StringLiteral S4, StringLiteral S5,
128+
StringLiteral S6, StringLiteral S7, StringLiteral S8,
129+
T Value) {
130+
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, Value);
131+
}
132+
133+
StringSwitch &Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2,
134+
StringLiteral S3, StringLiteral S4, StringLiteral S5,
135+
StringLiteral S6, StringLiteral S7, StringLiteral S8,
136+
StringLiteral S9, T Value) {
137+
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, S9, Value);
138+
}
139+
140+
// Case-insensitive case matchers.
141+
StringSwitch &CaseLower(StringLiteral S, T Value) {
142+
if (!Result && Str.equals_lower(S))
143+
Result = std::move(Value);
144+
145+
return *this;
146+
}
147+
148+
StringSwitch &EndsWithLower(StringLiteral S, T Value) {
149+
if (!Result && Str.endswith_lower(S))
150+
Result = Value;
151+
152+
return *this;
153+
}
154+
155+
StringSwitch &StartsWithLower(StringLiteral S, T Value) {
156+
if (!Result && Str.startswith_lower(S))
157+
Result = std::move(Value);
158+
159+
return *this;
160+
}
161+
162+
StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, T Value) {
163+
return CaseLower(S0, Value).CaseLower(S1, Value);
164+
}
165+
166+
StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
167+
T Value) {
168+
return CaseLower(S0, Value).CasesLower(S1, S2, Value);
169+
}
170+
171+
StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
172+
StringLiteral S3, T Value) {
173+
return CaseLower(S0, Value).CasesLower(S1, S2, S3, Value);
174+
}
175+
176+
StringSwitch &CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2,
177+
StringLiteral S3, StringLiteral S4, T Value) {
178+
return CaseLower(S0, Value).CasesLower(S1, S2, S3, S4, Value);
179+
}
180+
181+
LLVM_NODISCARD
182+
R Default(T Value) {
183+
if (Result)
184+
return std::move(*Result);
185+
return Value;
186+
}
187+
188+
LLVM_NODISCARD
189+
operator R() {
190+
assert(Result && "Fell off the end of a string-switch");
191+
return std::move(*Result);
192+
}
193+
};
194+
195+
} // end namespace llvm
196+
}}
197+
198+
#endif // LLVM_ADT_STRINGSWITCH_H

stdlib/public/Reflection/TypeRef.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ class DemanglingForTypeRef
491491
break;
492492
}
493493

494-
SmallVector<std::pair<NodePointer, bool>, 8> inputs;
494+
llvm::SmallVector<std::pair<NodePointer, bool>, 8> inputs;
495495
for (const auto &param : F->getParameters()) {
496496
auto flags = param.getFlags();
497497
auto input = visit(param.getType());

stdlib/public/runtime/Demangle.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ swift::_buildDemanglingForContext(const ContextDescriptor *context,
3434
NodePointer node = nullptr;
3535

3636
// Walk up the context tree.
37-
SmallVector<const ContextDescriptor *, 8> descriptorPath;
37+
llvm::SmallVector<const ContextDescriptor *, 8> descriptorPath;
3838
{
3939
const ContextDescriptor *parent = context;
4040
while (parent) {
@@ -285,11 +285,11 @@ _buildDemanglingForNominalType(const Metadata *type, Demangle::Demangler &Dem) {
285285

286286
// Gather the complete set of generic arguments that must be written to
287287
// form this type.
288-
SmallVector<const Metadata *, 8> allGenericArgs;
288+
llvm::SmallVector<const Metadata *, 8> allGenericArgs;
289289
gatherWrittenGenericArgs(type, description, allGenericArgs, Dem);
290290

291291
// Demangle the generic arguments.
292-
SmallVector<NodePointer, 8> demangledGenerics;
292+
llvm::SmallVector<NodePointer, 8> demangledGenerics;
293293
for (auto genericArg : allGenericArgs) {
294294
// When there is no generic argument, put in a placeholder.
295295
if (!genericArg) {
@@ -470,7 +470,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,
470470
break;
471471
}
472472

473-
SmallVector<std::pair<NodePointer, bool>, 8> inputs;
473+
llvm::SmallVector<std::pair<NodePointer, bool>, 8> inputs;
474474
for (unsigned i = 0, e = func->getNumParameters(); i < e; ++i) {
475475
auto param = func->getParameter(i);
476476
auto flags = func->getParameterFlags(i);

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,8 @@ _gatherGenericParameters(const ContextDescriptor *context,
988988
// requirements and fill in the generic arguments vector.
989989
if (!genericParamCounts.empty()) {
990990
// Compute the set of generic arguments "as written".
991-
SmallVector<const Metadata *, 8> allGenericArgs;
992-
991+
llvm::SmallVector<const Metadata *, 8> allGenericArgs;
992+
993993
// If we have a parent, gather it's generic arguments "as written".
994994
if (parent) {
995995
gatherWrittenGenericArgs(parent, parent->getTypeContextDescriptor(),
@@ -1183,16 +1183,16 @@ class DecodedMetadataBuilder {
11831183
if (!descriptor)
11841184
return BuiltType();
11851185
auto outerContext = descriptor->Parent.get();
1186-
1187-
SmallVector<BuiltType, 8> allGenericArgs;
1186+
1187+
llvm::SmallVector<BuiltType, 8> allGenericArgs;
11881188
for (auto argSet : genericArgs) {
11891189
allGenericArgs.append(argSet.begin(), argSet.end());
11901190
}
11911191

11921192
// Gather the generic parameters we need to parameterize the opaque decl.
1193-
SmallVector<unsigned, 8> genericParamCounts;
1194-
SmallVector<const void *, 8> allGenericArgsVec;
1195-
1193+
llvm::SmallVector<unsigned, 8> genericParamCounts;
1194+
llvm::SmallVector<const void *, 8> allGenericArgsVec;
1195+
11961196
if (!_gatherGenericParameters(outerContext,
11971197
allGenericArgs,
11981198
BuiltType(), /* no parent */
@@ -1291,8 +1291,8 @@ class DecodedMetadataBuilder {
12911291

12921292
// Figure out the various levels of generic parameters we have in
12931293
// this type.
1294-
SmallVector<unsigned, 8> genericParamCounts;
1295-
SmallVector<const void *, 8> allGenericArgsVec;
1294+
llvm::SmallVector<unsigned, 8> genericParamCounts;
1295+
llvm::SmallVector<const void *, 8> allGenericArgsVec;
12961296

12971297
if (!_gatherGenericParameters(typeDecl,
12981298
genericArgs,
@@ -1366,8 +1366,8 @@ class DecodedMetadataBuilder {
13661366
BuiltType
13671367
createFunctionType(llvm::ArrayRef<Demangle::FunctionParam<BuiltType>> params,
13681368
BuiltType result, FunctionTypeFlags flags) const {
1369-
SmallVector<BuiltType, 8> paramTypes;
1370-
SmallVector<uint32_t, 8> paramFlags;
1369+
llvm::SmallVector<BuiltType, 8> paramTypes;
1370+
llvm::SmallVector<uint32_t, 8> paramFlags;
13711371

13721372
// Fill in the parameters.
13731373
paramTypes.reserve(params.size());
@@ -2032,7 +2032,7 @@ void swift::gatherWrittenGenericArgs(
20322032
// canonicalized away. Use same-type requirements to reconstitute them.
20332033

20342034
// Retrieve the mapping information needed for depth/index -> flat index.
2035-
SmallVector<unsigned, 8> genericParamCounts;
2035+
llvm::SmallVector<unsigned, 8> genericParamCounts;
20362036
(void)_gatherGenericParameterCounts(description, genericParamCounts,
20372037
BorrowFrom);
20382038

stdlib/public/runtime/ProtocolConformance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ template<>
162162
const WitnessTable *
163163
ProtocolConformanceDescriptor::getWitnessTable(const Metadata *type) const {
164164
// If needed, check the conditional requirements.
165-
SmallVector<const void *, 8> conditionalArgs;
165+
llvm::SmallVector<const void *, 8> conditionalArgs;
166166
if (hasConditionalRequirements()) {
167167
SubstGenericParametersFromMetadata substitutions(type);
168168
bool failed =

0 commit comments

Comments
 (0)