Skip to content

Commit 95af3be

Browse files
committed
AST: Eliminate PackConformanceExpander
1 parent ce62a2e commit 95af3be

File tree

1 file changed

+31
-64
lines changed

1 file changed

+31
-64
lines changed

lib/AST/PackConformance.cpp

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -172,66 +172,6 @@ ProtocolConformanceRef PackConformance::subst(SubstitutionMap subMap,
172172
return subst(IFS);
173173
}
174174

175-
namespace {
176-
177-
struct PackConformanceExpander {
178-
InFlightSubstitution &IFS;
179-
ArrayRef<ProtocolConformanceRef> origConformances;
180-
181-
public:
182-
// Results built up by the expansion.
183-
SmallVector<Type, 4> substElementTypes;
184-
SmallVector<ProtocolConformanceRef, 4> substConformances;
185-
186-
PackConformanceExpander(InFlightSubstitution &IFS,
187-
ArrayRef<ProtocolConformanceRef> origConformances)
188-
: IFS(IFS), origConformances(origConformances) {}
189-
190-
private:
191-
/// Substitute a scalar element of the original pack.
192-
void substScalar(Type origElementType,
193-
ProtocolConformanceRef origConformance) {
194-
auto substElementType = origElementType.subst(IFS);
195-
auto substConformance = origConformance.subst(origElementType, IFS);
196-
197-
substElementTypes.push_back(substElementType);
198-
substConformances.push_back(substConformance);
199-
}
200-
201-
/// Substitute and expand an expansion element of the original pack.
202-
void substExpansion(PackExpansionType *origExpansionType,
203-
ProtocolConformanceRef origConformance) {
204-
IFS.expandPackExpansionType(origExpansionType,
205-
[&](Type substComponentType) {
206-
auto origPatternType = origExpansionType->getPatternType();
207-
208-
// Just substitute the conformance. We don't directly represent
209-
// pack expansion conformances here; it's sort of implicit in the
210-
// corresponding pack element type.
211-
auto substConformance = origConformance.subst(origPatternType, IFS);
212-
213-
substElementTypes.push_back(substComponentType);
214-
substConformances.push_back(substConformance);
215-
});
216-
}
217-
218-
public:
219-
void expand(PackType *origPackType) {
220-
assert(origPackType->getNumElements() == origConformances.size());
221-
222-
for (auto i : range(origPackType->getNumElements())) {
223-
auto origElementType = origPackType->getElementType(i);
224-
if (auto *origExpansion = origElementType->getAs<PackExpansionType>()) {
225-
substExpansion(origExpansion, origConformances[i]);
226-
} else {
227-
substScalar(origElementType, origConformances[i]);
228-
}
229-
}
230-
}
231-
};
232-
233-
} // end anonymous namespace
234-
235175
ProtocolConformanceRef PackConformance::subst(TypeSubstitutionFn subs,
236176
LookupConformanceFn conformances,
237177
SubstOptions options) const {
@@ -241,14 +181,41 @@ ProtocolConformanceRef PackConformance::subst(TypeSubstitutionFn subs,
241181

242182
ProtocolConformanceRef
243183
PackConformance::subst(InFlightSubstitution &IFS) const {
244-
PackConformanceExpander expander(IFS, getPatternConformances());
245-
expander.expand(ConformingType);
184+
// Results built up by the expansion.
185+
SmallVector<Type, 4> substElementTypes;
186+
SmallVector<ProtocolConformanceRef, 4> substConformances;
187+
188+
auto origConformances = getPatternConformances();
189+
assert(ConformingType->getNumElements() == origConformances.size());
190+
191+
for (auto i : range(ConformingType->getNumElements())) {
192+
auto origElementType = ConformingType->getElementType(i);
193+
if (auto *origExpansion = origElementType->getAs<PackExpansionType>()) {
194+
// Substitute and expand an expansion element of the original pack.
195+
IFS.expandPackExpansionType(origExpansion,
196+
[&](Type substComponentType) {
197+
substElementTypes.push_back(substComponentType);
198+
199+
// Just substitute the conformance. We don't directly represent
200+
// pack expansion conformances here; it's sort of implicit in the
201+
// corresponding pack element type.
202+
substConformances.push_back(
203+
origConformances[i].subst(origExpansion->getPatternType(), IFS));
204+
});
205+
} else {
206+
// Substitute a scalar element of the original pack.
207+
substElementTypes.push_back(origElementType.subst(IFS));
208+
209+
substConformances.push_back(
210+
origConformances[i].subst(origElementType, IFS));
211+
}
212+
}
246213

247214
auto &ctx = Protocol->getASTContext();
248-
auto *substConformingType = PackType::get(ctx, expander.substElementTypes);
215+
auto *substConformingType = PackType::get(ctx, substElementTypes);
249216

250217
auto substConformance = PackConformance::get(substConformingType, Protocol,
251-
expander.substConformances);
218+
substConformances);
252219
return ProtocolConformanceRef(substConformance);
253220
}
254221

0 commit comments

Comments
 (0)