Skip to content

Commit 8db8b7c

Browse files
committed
[GenericEnvironment] Add a method to retrieve the bindings from pack element
archetypes to their originating pack archetypes.
1 parent 8ec5405 commit 8db8b7c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
190190
/// Retrieve the UUID for an opened element environment.
191191
UUID getOpenedElementUUID() const;
192192

193+
using PackElementBinding =
194+
std::pair<ElementArchetypeType *, PackArchetypeType *>;
195+
196+
/// Retrieve the bindings for the opened pack element archetypes in this
197+
/// generic environment to the pack archetypes that contain them.
198+
///
199+
/// \param bindings The vector to populate with the pack element bindings.
200+
void getPackElementBindings(
201+
SmallVectorImpl<PackElementBinding> &bindings) const;
202+
193203
/// Create a new, primary generic environment.
194204
static GenericEnvironment *forPrimary(GenericSignature signature);
195205

lib/AST/GenericEnvironment.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,33 @@ UUID GenericEnvironment::getOpenedElementUUID() const {
125125
return getTrailingObjects<OpenedElementEnvironmentData>()->uuid;
126126
}
127127

128+
void GenericEnvironment::getPackElementBindings(
129+
SmallVectorImpl<PackElementBinding> &bindings) const {
130+
auto packElements = getGenericSignature().getInnermostGenericParams();
131+
auto packElementDepth = packElements.front()->getDepth();
132+
auto elementIt = packElements.begin();
133+
134+
// Each parameter pack in the outer generic parameters has
135+
// a corresponding pack element parameter at the innermost
136+
// depth.
137+
for (auto *genericParam : getGenericParams()) {
138+
if (genericParam->getDepth() == packElementDepth)
139+
break;
140+
141+
if (!genericParam->isParameterPack())
142+
continue;
143+
144+
assert(elementIt != packElements.end());
145+
auto *elementArchetype =
146+
mapTypeIntoContext(*elementIt++)->getAs<ElementArchetypeType>();
147+
auto *packArchetype =
148+
mapTypeIntoContext(genericParam)->getAs<PackArchetypeType>();
149+
150+
assert(elementArchetype && packArchetype);
151+
bindings.emplace_back(elementArchetype, packArchetype);
152+
}
153+
}
154+
128155
GenericEnvironment::GenericEnvironment(GenericSignature signature)
129156
: SignatureAndKind(signature, Kind::Primary)
130157
{

0 commit comments

Comments
 (0)