Skip to content

Commit fb8294b

Browse files
committed
Make the API for iterating packs in an opened environment callback-based
This is generally easier for clients to work with.
1 parent f2e9c2d commit fb8294b

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,19 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
195195
/// Retrieve the UUID for an opened element environment.
196196
UUID getOpenedElementUUID() const;
197197

198-
using PackElementBinding =
199-
std::pair<ElementArchetypeType *, PackArchetypeType *>;
200-
201-
/// Retrieve the bindings for the opened pack element archetypes in this
202-
/// generic environment to the pack archetypes that contain them.
203-
///
204-
/// \param bindings The vector to populate with the pack element bindings.
205-
void getPackElementBindings(
206-
SmallVectorImpl<PackElementBinding> &bindings) const;
198+
void forEachPackElementArchetype(
199+
llvm::function_ref<void(ElementArchetypeType*)> function) const;
200+
201+
using PackElementBindingCallback =
202+
llvm::function_ref<void(ElementArchetypeType *elementType,
203+
PackType *packSubstitution)>;
204+
205+
/// Given that this is an opened element environment, iterate the
206+
/// opened pack element bindings: the pack archetype that's been opened
207+
/// (which may not be meaningful in the surrounding context), the element
208+
/// archetype that it has been opened as, and the pack type whose elements
209+
/// are opened.
210+
void forEachPackElementBinding(PackElementBindingCallback function) const;
207211

208212
/// Create a new, primary generic environment.
209213
static GenericEnvironment *forPrimary(GenericSignature signature);

lib/AST/GenericEnvironment.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,18 @@ UUID GenericEnvironment::getOpenedElementUUID() const {
131131
return getTrailingObjects<OpenedElementEnvironmentData>()->uuid;
132132
}
133133

134-
void GenericEnvironment::getPackElementBindings(
135-
SmallVectorImpl<PackElementBinding> &bindings) const {
134+
void GenericEnvironment::forEachPackElementArchetype(
135+
llvm::function_ref<void(ElementArchetypeType *)> function) const {
136+
auto packElements = getGenericSignature().getInnermostGenericParams();
137+
for (auto eltInterfaceType: packElements) {
138+
auto *elementArchetype =
139+
mapTypeIntoContext(eltInterfaceType)->castTo<ElementArchetypeType>();
140+
function(elementArchetype);
141+
}
142+
}
143+
144+
void GenericEnvironment::forEachPackElementBinding(
145+
PackElementBindingCallback function) const {
136146
auto sig = getGenericSignature();
137147
auto shapeClass = getOpenedElementShapeClass();
138148
auto packElements = sig.getInnermostGenericParams();
@@ -156,13 +166,13 @@ void GenericEnvironment::getPackElementBindings(
156166

157167
assert(elementIt != packElements.end());
158168
auto *elementArchetype =
159-
mapTypeIntoContext(*elementIt++)->getAs<ElementArchetypeType>();
160-
auto *packArchetype =
161-
mapTypeIntoContext(genericParam)->getAs<PackArchetypeType>();
162-
163-
assert(elementArchetype && packArchetype);
164-
bindings.emplace_back(elementArchetype, packArchetype);
169+
mapTypeIntoContext(*elementIt++)->castTo<ElementArchetypeType>();
170+
auto *packSubstitution =
171+
maybeApplyOuterContextSubstitutions(genericParam)->castTo<PackType>();
172+
function(elementArchetype, packSubstitution);
165173
}
174+
175+
assert(elementIt == packElements.end());
166176
}
167177

168178
GenericEnvironment::GenericEnvironment(GenericSignature signature)

0 commit comments

Comments
 (0)