Skip to content

Commit 81d9e68

Browse files
committed
Add a couple convenience APIs for working with abstraction patterns
1 parent 70e83b5 commit 81d9e68

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

include/swift/SIL/AbstractionPattern.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef SWIFT_SIL_ABSTRACTIONPATTERN_H
1919
#define SWIFT_SIL_ABSTRACTIONPATTERN_H
2020

21+
#include "swift/Basic/IndexedViewRange.h"
2122
#include "swift/AST/Decl.h"
2223
#include "swift/AST/Types.h"
2324

@@ -1332,6 +1333,17 @@ class AbstractionPattern {
13321333
llvm_unreachable("bad kind");
13331334
}
13341335

1336+
static AbstractionPattern
1337+
projectTupleElementType(const AbstractionPattern *base, size_t index) {
1338+
return base->getTupleElementType(index);
1339+
}
1340+
1341+
IndexedViewRange<const AbstractionPattern *, AbstractionPattern,
1342+
projectTupleElementType> getTupleElementTypes() const {
1343+
assert(isTuple());
1344+
return { { this, 0 }, { this, getNumTupleElements() } };
1345+
}
1346+
13351347
/// Is the given pack type a valid substitution of this abstraction
13361348
/// pattern?
13371349
bool matchesPack(CanPackType substType);
@@ -1472,6 +1484,8 @@ class AbstractionPattern {
14721484
void forEachPackExpandedComponent(
14731485
llvm::function_ref<void(AbstractionPattern pattern)> fn) const;
14741486

1487+
SmallVector<AbstractionPattern, 4> getPackExpandedComponents() const;
1488+
14751489
/// If this pattern refers to a foreign ObjC method that was imported as
14761490
/// async, return the bridged-back-to-ObjC completion handler type.
14771491
CanType getObjCMethodAsyncCompletionHandlerForeignType(

lib/SIL/IR/AbstractionPattern.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,15 @@ AbstractionPattern AbstractionPattern::getPackExpansionCountType() const {
588588
llvm_unreachable("bad kind");
589589
}
590590

591+
SmallVector<AbstractionPattern, 4>
592+
AbstractionPattern::getPackExpandedComponents() const {
593+
SmallVector<AbstractionPattern, 4> result;
594+
forEachPackExpandedComponent([&](AbstractionPattern pattern) {
595+
result.push_back(pattern);
596+
});
597+
return result;
598+
}
599+
591600
void AbstractionPattern::forEachPackExpandedComponent(
592601
llvm::function_ref<void (AbstractionPattern)> fn) const {
593602
assert(isPackExpansion());

lib/SILGen/SILGenApply.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,11 +3229,8 @@ class ArgEmitter {
32293229

32303230
// Otherwise we need to emit a pack argument.
32313231
} else {
3232-
SmallVector<AbstractionPattern, 4> origPackEltPatterns;
3233-
origFormalParamType.forEachPackExpandedComponent(
3234-
[&](AbstractionPattern pattern) {
3235-
origPackEltPatterns.push_back(pattern);
3236-
});
3232+
auto origPackEltPatterns =
3233+
origFormalParamType.getPackExpandedComponents();
32373234

32383235
auto argSourcesSlice =
32393236
argSources.slice(nextArgSourceIndex, origPackEltPatterns.size());

lib/SILGen/SILGenStmt.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,11 +504,7 @@ preparePackResultInit(SILGenFunction &SGF, SILLocation loc,
504504
SmallVectorImpl<CleanupHandle> &cleanups,
505505
SmallVectorImpl<InitializationPtr> &inits) {
506506
assert(origExpansionType.isPackExpansion());
507-
SmallVector<AbstractionPattern, 4> origComponentTypes;
508-
origExpansionType.forEachPackExpandedComponent(
509-
[&](AbstractionPattern component) {
510-
origComponentTypes.push_back(component);
511-
});
507+
auto origComponentTypes = origExpansionType.getPackExpandedComponents();
512508

513509
auto loweredPackType = packAddr->getType().castTo<SILPackType>();
514510
assert(loweredPackType->getNumElements() == origComponentTypes.size() &&

0 commit comments

Comments
 (0)