Skip to content

Commit 68df7e8

Browse files
committed
[NFC] Move this helper for opening pack expansions into the AST
1 parent a6d2a34 commit 68df7e8

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ class OpaqueTypeDecl;
3838
class ElementArchetypeType;
3939
class OpenedArchetypeType;
4040
class PackArchetypeType;
41+
class PackExpansionType;
4142
class SILModule;
4243
class SILType;
44+
template <class> class CanTypeWrapper;
4345

4446
/// Query function suitable for use as a \c TypeSubstitutionFn that queries
4547
/// the mapping of interface types to archetypes.
@@ -310,6 +312,23 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
310312

311313
SWIFT_DEBUG_DUMP;
312314
};
315+
316+
/// A pair of an opened-element generic signature and an opened-element
317+
/// generic environment.
318+
struct OpenedElementContext {
319+
/// The opened-element environment for this expansion.
320+
GenericEnvironment *environment;
321+
322+
/// The opened-element signature for this expansion.
323+
CanGenericSignature signature;
324+
325+
/// Create a fresh opened element context from a contextual pack
326+
/// expansion type. This is useful when writing code that needs to
327+
/// break down the components of a pack expansion.
328+
static OpenedElementContext
329+
createForContextualExpansion(ASTContext &ctx,
330+
CanTypeWrapper<PackExpansionType> expansionType);
331+
};
313332

314333
} // end namespace swift
315334

lib/AST/GenericEnvironment.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,24 @@ GenericEnvironment::mapConformanceRefIntoContext(
709709
auto contextType = mapTypeIntoContext(conformingInterfaceType);
710710
return {contextType, contextConformance};
711711
}
712+
713+
OpenedElementContext
714+
OpenedElementContext::createForContextualExpansion(ASTContext &ctx,
715+
CanPackExpansionType expansionType) {
716+
assert(!expansionType->hasTypeParameter() &&
717+
"must be given a contextual type");
718+
719+
// Get the outer generic signature and environment.
720+
auto *genericEnv = cast<ArchetypeType>(expansionType.getCountType())
721+
->getGenericEnvironment();
722+
auto subMap = genericEnv->getForwardingSubstitutionMap();
723+
724+
auto genericSig = genericEnv->getGenericSignature().getCanonicalSignature();
725+
// Create an opened element signature and environment.
726+
auto elementSig = ctx.getOpenedElementSignature(
727+
genericSig, expansionType.getCountType());
728+
auto *elementEnv = GenericEnvironment::forOpenedElement(
729+
elementSig, UUID::fromTime(), expansionType.getCountType(), subMap);
730+
731+
return {elementEnv, elementSig};
732+
}

lib/IRGen/GenPack.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -261,28 +261,6 @@ static llvm::Value *bindWitnessTableAtIndex(IRGenFunction &IGF,
261261
return wtable;
262262
}
263263

264-
struct OpenedElementContext {
265-
GenericEnvironment *environment;
266-
CanGenericSignature signature;
267-
268-
static OpenedElementContext
269-
createForPackExpansion(IRGenFunction &IGF, CanPackExpansionType expansionTy) {
270-
// Get the outer generic signature and environment.
271-
auto *genericEnv = cast<ArchetypeType>(expansionTy.getCountType())
272-
->getGenericEnvironment();
273-
auto subMap = genericEnv->getForwardingSubstitutionMap();
274-
275-
auto genericSig = genericEnv->getGenericSignature().getCanonicalSignature();
276-
// Create an opened element signature and environment.
277-
auto elementSig = IGF.IGM.Context.getOpenedElementSignature(
278-
genericSig, expansionTy.getCountType());
279-
auto *elementEnv = GenericEnvironment::forOpenedElement(
280-
elementSig, UUID::fromTime(), expansionTy.getCountType(), subMap);
281-
282-
return {elementEnv, elementSig};
283-
}
284-
};
285-
286264
static void bindElementSignatureRequirementsAtIndex(
287265
IRGenFunction &IGF, OpenedElementContext const &context, llvm::Value *index,
288266
DynamicMetadataRequest request) {
@@ -417,7 +395,7 @@ static void emitPackExpansionMetadataPack(IRGenFunction &IGF, Address pack,
417395
emitPackExpansionPack(
418396
IGF, pack, expansionTy, dynamicIndex, dynamicLength, [&](auto *index) {
419397
auto context =
420-
OpenedElementContext::createForPackExpansion(IGF, expansionTy);
398+
OpenedElementContext::createForContextualExpansion(IGF.IGM.Context, expansionTy);
421399
auto patternTy = expansionTy.getPatternType();
422400
return emitPackExpansionElementMetadata(IGF, context, patternTy, index,
423401
request);
@@ -544,7 +522,7 @@ static void emitPackExpansionWitnessTablePack(
544522
IGF, pack, expansionTy, dynamicIndex, dynamicLength, [&](auto *index) {
545523
llvm::Value *_metadata = nullptr;
546524
auto context =
547-
OpenedElementContext::createForPackExpansion(IGF, expansionTy);
525+
OpenedElementContext::createForContextualExpansion(IGF.IGM.Context, expansionTy);
548526
auto patternTy = expansionTy.getPatternType();
549527
return emitPackExpansionElementWitnessTable(
550528
IGF, context, patternTy, conformance,
@@ -862,7 +840,7 @@ llvm::Value *irgen::emitTypeMetadataPackElementRef(
862840
// pack expansion at that index.
863841
auto *relativeIndex = IGF.Builder.CreateSub(index, lowerBound);
864842
auto context =
865-
OpenedElementContext::createForPackExpansion(IGF, expansionTy);
843+
OpenedElementContext::createForContextualExpansion(IGF.IGM.Context, expansionTy);
866844
auto patternTy = expansionTy.getPatternType();
867845
metadata = emitPackExpansionElementMetadata(IGF, context, patternTy,
868846
relativeIndex, request);

0 commit comments

Comments
 (0)