Skip to content

Commit 6dc298a

Browse files
committed
[NFC] Split the inner/outer vanishing tuple handling
I'm going to change the type signature for inner tuples, so this isn't going to work.
1 parent 8a5bfd5 commit 6dc298a

File tree

1 file changed

+69
-36
lines changed

1 file changed

+69
-36
lines changed

lib/SILGen/SILGenPoly.cpp

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -935,13 +935,21 @@ class ExpanderBase {
935935
AbstractionPattern outerOrigType,
936936
CanType outerSubstType);
937937

938-
void expandVanishingTuple(AbstractionPattern origType, CanType substType,
939-
bool forInner,
940-
llvm::function_ref<void(AbstractionPattern origType,
941-
CanType substType)> handleSingle,
942-
llvm::function_ref<void(AbstractionPattern origType,
943-
CanType substType,
944-
ManagedValue eltResult)> handlePackElement);
938+
void expandInnerVanishingTuple(AbstractionPattern innerOrigType,
939+
CanType innerSubstType,
940+
llvm::function_ref<void(AbstractionPattern innerOrigEltType,
941+
CanType innerSubstEltType)> handleSingle,
942+
llvm::function_ref<void(AbstractionPattern innerOrigEltType,
943+
CanType innerSubstEltType,
944+
ManagedValue innerEltAddr)> handlePackElement);
945+
946+
void expandOuterVanishingTuple(AbstractionPattern outerOrigType,
947+
CanType outerSubstType,
948+
llvm::function_ref<void(AbstractionPattern outerOrigEltType,
949+
CanType outerSubstEltType)> handleSingle,
950+
llvm::function_ref<void(AbstractionPattern outerOrigEltType,
951+
CanType outerSubstEltType,
952+
ManagedValue outerEltAddr)> handlePackElement);
945953

946954
void expandParallelTuples(AbstractionPattern innerOrigType,
947955
CanType innerSubstType,
@@ -3102,8 +3110,7 @@ void ExpanderBase<Impl>::expand(AbstractionPattern innerOrigType,
31023110
// through that and recurse.
31033111
bool outerIsExpanded = outerOrigType.isTuple();
31043112
if (outerIsExpanded && outerOrigType.doesTupleVanish()) {
3105-
asImpl().expandVanishingTuple(outerOrigType, outerSubstType,
3106-
/*inner*/ false,
3113+
asImpl().expandOuterVanishingTuple(outerOrigType, outerSubstType,
31073114
[&](AbstractionPattern outerOrigEltType, CanType outerSubstEltType) {
31083115
asImpl().expand(innerOrigType, innerSubstType,
31093116
outerOrigEltType, outerSubstEltType);
@@ -3120,7 +3127,7 @@ void ExpanderBase<Impl>::expand(AbstractionPattern innerOrigType,
31203127
// through that and recurse.
31213128
bool innerIsExpanded = innerOrigType.isTuple();
31223129
if (innerIsExpanded && innerOrigType.doesTupleVanish()) {
3123-
expandVanishingTuple(innerOrigType, innerSubstType, /*inner*/ true,
3130+
expandInnerVanishingTuple(innerOrigType, innerSubstType,
31243131
[&](AbstractionPattern innerOrigEltType, CanType innerSubstEltType) {
31253132
asImpl().expand(innerOrigEltType, innerSubstEltType,
31263133
outerOrigType, outerSubstType);
@@ -3149,43 +3156,69 @@ void ExpanderBase<Impl>::expand(AbstractionPattern innerOrigType,
31493156
}
31503157

31513158
template <class Impl>
3152-
void ExpanderBase<Impl>::expandVanishingTuple(AbstractionPattern origType,
3153-
CanType substType,
3154-
bool forInner,
3155-
llvm::function_ref<void(AbstractionPattern origType,
3156-
CanType substType)> handleSingle,
3157-
llvm::function_ref<void(AbstractionPattern origType,
3158-
CanType substType,
3159-
ManagedValue eltValue)> handlePackElement) {
3159+
void ExpanderBase<Impl>::expandOuterVanishingTuple(
3160+
AbstractionPattern outerOrigType,
3161+
CanType outerSubstType,
3162+
llvm::function_ref<void(AbstractionPattern outerOrigEltType,
3163+
CanType outerSubstEltType)> handleSingle,
3164+
llvm::function_ref<void(AbstractionPattern outerOrigEltType,
3165+
CanType outerOrigSubstType,
3166+
ManagedValue outerEltAddr)> handlePackElement) {
3167+
assert(outerOrigType.isTuple());
3168+
assert(outerOrigType.doesTupleVanish());
31603169

31613170
bool foundSurvivor = false;
31623171

3163-
auto packInputs = [&]() -> PackGeneratorRef {
3164-
if (forInner) {
3165-
return asImpl().getInnerPackGenerator();
3172+
ExpandedTupleInputGenerator elt(SGF.getASTContext(), OuterPackArgs,
3173+
outerOrigType, outerSubstType);
3174+
for (; !elt.isFinished(); elt.advance()) {
3175+
assert(!foundSurvivor);
3176+
foundSurvivor = true;
3177+
3178+
if (!elt.isOrigPackExpansion()) {
3179+
handleSingle(elt.getOrigType(), outerSubstType);
31663180
} else {
3167-
return OuterPackArgs;
3181+
assert(elt.getPackComponentIndex() == 0);
3182+
assert(!isa<PackExpansionType>(elt.getSubstType()));
3183+
ManagedValue eltAddr = elt.projectPackComponent(SGF, Loc);
3184+
handlePackElement(elt.getOrigType().getPackExpansionPatternType(),
3185+
outerSubstType, eltAddr);
31683186
}
3169-
}();
3187+
}
3188+
elt.finish();
3189+
3190+
assert(foundSurvivor && "vanishing tuple had no surviving element?");
3191+
}
3192+
3193+
template <class Impl>
3194+
void ExpanderBase<Impl>::expandInnerVanishingTuple(
3195+
AbstractionPattern innerOrigType,
3196+
CanType innerSubstType,
3197+
llvm::function_ref<void(AbstractionPattern innerOrigEltType,
3198+
CanType innerSubstEltType)> handleSingle,
3199+
llvm::function_ref<void(AbstractionPattern innerOrigEltType,
3200+
CanType innerOrigSubstType,
3201+
ManagedValue innerEltAddr)> handlePackElement) {
3202+
assert(innerOrigType.isTuple());
3203+
assert(innerOrigType.doesTupleVanish());
3204+
3205+
bool foundSurvivor = false;
31703206

3171-
ExpandedTupleInputGenerator elt(SGF.getASTContext(), packInputs,
3172-
origType, substType);
3207+
ExpandedTupleInputGenerator elt(SGF.getASTContext(),
3208+
asImpl().getInnerPackGenerator(),
3209+
innerOrigType, innerSubstType);
31733210
for (; !elt.isFinished(); elt.advance()) {
31743211
assert(!foundSurvivor);
31753212
foundSurvivor = true;
31763213

31773214
if (!elt.isOrigPackExpansion()) {
3178-
handleSingle(elt.getOrigType(), elt.getSubstType());
3215+
handleSingle(elt.getOrigType(), innerSubstType);
31793216
} else {
31803217
assert(elt.getPackComponentIndex() == 0);
31813218
assert(!isa<PackExpansionType>(elt.getSubstType()));
3182-
ManagedValue eltAddr = [&] {
3183-
if (forInner)
3184-
return elt.createPackComponentTemporary(SGF, Loc);
3185-
return elt.projectPackComponent(SGF, Loc);
3186-
}();
3219+
ManagedValue eltAddr = elt.createPackComponentTemporary(SGF, Loc);
31873220
handlePackElement(elt.getOrigType().getPackExpansionPatternType(),
3188-
substType, eltAddr);
3221+
innerSubstType, eltAddr);
31893222
}
31903223
}
31913224
elt.finish();
@@ -3604,7 +3637,7 @@ void ExpanderBase<Impl>::expandOuterIndirect(
36043637

36053638
// Otherwise, we have a vanishing tuple. Expand it, find the surviving
36063639
// element, and recurse.
3607-
asImpl().expandVanishingTuple(innerOrigType, innerSubstType, /*inner*/ true,
3640+
asImpl().expandInnerVanishingTuple(innerOrigType, innerSubstType,
36083641
[&](AbstractionPattern innerOrigEltType, CanType innerSubstEltType) {
36093642
asImpl().expandOuterIndirect(innerOrigEltType, innerSubstEltType,
36103643
outerOrigType, outerSubstType,
@@ -3783,7 +3816,7 @@ void ResultPlanner::planIntoDirect(AbstractionPattern innerOrigType,
37833816

37843817
// Otherwise, the inner tuple vanishes. Expand it, find the surviving
37853818
// element, and recurse.
3786-
expandVanishingTuple(innerOrigType, innerSubstType, /*inner*/ true,
3819+
expandInnerVanishingTuple(innerOrigType, innerSubstType,
37873820
[&](AbstractionPattern innerOrigEltType, CanType innerSubstEltType) {
37883821
planIntoDirect(innerOrigEltType, innerSubstEltType,
37893822
outerOrigType, outerSubstType,
@@ -4025,7 +4058,7 @@ void ExpanderBase<Impl>::expandInnerIndirect(AbstractionPattern innerOrigType,
40254058

40264059
// Otherwise, the outer pattern is a vanishing tuple. Expand it,
40274060
// find the surviving element, and recurse.
4028-
asImpl().expandVanishingTuple(outerOrigType, outerSubstType, /*inner*/ false,
4061+
asImpl().expandOuterVanishingTuple(outerOrigType, outerSubstType,
40294062
[&](AbstractionPattern outerOrigEltType, CanType outerSubstEltType) {
40304063
asImpl().expandInnerIndirect(innerOrigType, innerSubstType,
40314064
outerOrigEltType, outerSubstEltType,
@@ -4208,7 +4241,7 @@ void ResultPlanner::planFromDirect(AbstractionPattern innerOrigType,
42084241

42094242
// Otherwise, expand the outer tuple and recurse for the surviving
42104243
// element.
4211-
expandVanishingTuple(outerOrigType, outerSubstType, /*inner*/ false,
4244+
expandOuterVanishingTuple(outerOrigType, outerSubstType,
42124245
[&](AbstractionPattern outerOrigEltType, CanType outerSubstEltType) {
42134246
planFromDirect(innerOrigType, innerSubstType,
42144247
outerOrigEltType, outerSubstEltType,

0 commit comments

Comments
 (0)