Skip to content

Commit 502bbd0

Browse files
committed
[GenericEnvironment] API to iterate pack element
The function `forEachPackElementBinding` found "interesting" GenericTypeParamTypes, transformed each, and called back. Here the work of finding such "interesting" types is pulled out into a separate function `forEachPackElementGenericTypeParam` through which `forEachPackElementBinding` now factors.
1 parent c8d57c8 commit 502bbd0

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
200200
void forEachPackElementArchetype(
201201
llvm::function_ref<void(ElementArchetypeType*)> function) const;
202202

203+
void forEachPackElementGenericTypeParam(
204+
llvm::function_ref<void(GenericTypeParamType *)> function) const;
205+
203206
using PackElementBindingCallback =
204207
llvm::function_ref<void(ElementArchetypeType *elementType,
205208
PackType *packSubstitution)>;

lib/AST/GenericEnvironment.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,12 @@ void GenericEnvironment::forEachPackElementArchetype(
141141
}
142142
}
143143

144-
void GenericEnvironment::forEachPackElementBinding(
145-
PackElementBindingCallback function) const {
144+
void GenericEnvironment::forEachPackElementGenericTypeParam(
145+
llvm::function_ref<void(GenericTypeParamType *)> function) const {
146146
auto sig = getGenericSignature();
147147
auto shapeClass = getOpenedElementShapeClass();
148148
auto packElements = sig.getInnermostGenericParams();
149149
auto packElementDepth = packElements.front()->getDepth();
150-
auto elementIt = packElements.begin();
151150

152151
// Each parameter pack in the outer generic parameters has
153152
// a corresponding pack element parameter at the innermost
@@ -164,13 +163,23 @@ void GenericEnvironment::forEachPackElementBinding(
164163
if (!sig->haveSameShape(genericParam, shapeClass->mapTypeOutOfContext()))
165164
continue;
166165

166+
function(genericParam);
167+
}
168+
}
169+
170+
void GenericEnvironment::forEachPackElementBinding(
171+
PackElementBindingCallback function) const {
172+
auto sig = getGenericSignature();
173+
auto packElements = sig.getInnermostGenericParams();
174+
auto elementIt = packElements.begin();
175+
forEachPackElementGenericTypeParam([&](auto *genericParam) {
167176
assert(elementIt != packElements.end());
168177
auto *elementArchetype =
169178
mapTypeIntoContext(*elementIt++)->castTo<ElementArchetypeType>();
170-
auto *packSubstitution =
171-
maybeApplyOuterContextSubstitutions(genericParam)->castTo<PackType>();
179+
auto *packSubstitution = maybeApplyOuterContextSubstitutions(genericParam)
180+
->template castTo<PackType>();
172181
function(elementArchetype, packSubstitution);
173-
}
182+
});
174183

175184
assert(elementIt == packElements.end());
176185
}

0 commit comments

Comments
 (0)