Skip to content

Commit 5f1a669

Browse files
committed
[NFC] Change the existing client of forEachFunctionParam
Yeah, this is much cleaner.
1 parent 2e4ad65 commit 5f1a669

File tree

3 files changed

+38
-70
lines changed

3 files changed

+38
-70
lines changed

include/swift/SIL/AbstractionPattern.h

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace clang {
3535

3636
namespace swift {
3737
namespace Lowering {
38+
class FunctionParamGenerator;
3839

3940
/// A pattern for the abstraction of a value.
4041
///
@@ -1501,29 +1502,20 @@ class AbstractionPattern {
15011502
/// parameters in the pattern.
15021503
unsigned getNumFunctionParams() const;
15031504

1504-
/// Perform a parallel visitation of the parameters of a function.
1505-
///
1506-
/// If this is a function pattern, calls handleScalar or
1507-
/// handleExpansion as appropriate for each parameter of the
1508-
/// original function, in order.
1505+
/// Traverses the parameters of a function, where this is the
1506+
/// abstraction pattern for the function (its "original type")
1507+
/// and the given parameters are the substituted formal parameters.
1508+
/// Calls the callback once for each parameter in the abstraction
1509+
/// pattern.
15091510
///
15101511
/// If this is not a function pattern, calls handleScalar for each
1511-
/// parameter of the substituted function type. Functions with
1512-
/// pack expansions cannot be abstracted legally this way.
1512+
/// parameter of the substituted function type. Note that functions
1513+
/// with pack expansions cannot be legally abstracted this way; it
1514+
/// is not possible in Swift's ABI to support this without some sort
1515+
/// of dynamic argument-forwarding thunk.
15131516
void forEachFunctionParam(AnyFunctionType::CanParamArrayRef substParams,
15141517
bool ignoreFinalParam,
1515-
llvm::function_ref<void(unsigned origParamIndex,
1516-
unsigned substParamIndex,
1517-
ParameterTypeFlags origFlags,
1518-
AbstractionPattern origParamType,
1519-
AnyFunctionType::CanParam substParam)>
1520-
handleScalar,
1521-
llvm::function_ref<void(unsigned origParamIndex,
1522-
unsigned substParamIndex,
1523-
ParameterTypeFlags origFlags,
1524-
AbstractionPattern origExpansionType,
1525-
AnyFunctionType::CanParamArrayRef substParams)>
1526-
handleExpansion) const;
1518+
llvm::function_ref<void(FunctionParamGenerator &param)> function) const;
15271519

15281520
/// Given that the value being abstracted is optional, return the
15291521
/// abstraction pattern for its object type.

lib/SIL/IR/AbstractionPattern.cpp

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,34 +1204,10 @@ unsigned AbstractionPattern::getNumFunctionParams() const {
12041204
void AbstractionPattern::
12051205
forEachFunctionParam(AnyFunctionType::CanParamArrayRef substParams,
12061206
bool ignoreFinalOrigParam,
1207-
llvm::function_ref<void(unsigned origParamIndex,
1208-
unsigned substParamIndex,
1209-
ParameterTypeFlags origFlags,
1210-
AbstractionPattern origParamType,
1211-
AnyFunctionType::CanParam substParam)>
1212-
handleScalar,
1213-
llvm::function_ref<void(unsigned origParamIndex,
1214-
unsigned substParamIndex,
1215-
ParameterTypeFlags origFlags,
1216-
AbstractionPattern origExpansionType,
1217-
AnyFunctionType::CanParamArrayRef substParams)>
1218-
handleExpansion) const {
1207+
llvm::function_ref<void(FunctionParamGenerator &param)> function) const {
12191208
FunctionParamGenerator generator(*this, substParams, ignoreFinalOrigParam);
1220-
12211209
for (; !generator.isFinished(); generator.advance()) {
1222-
if (generator.isPackExpansion()) {
1223-
handleExpansion(generator.getOrigIndex(),
1224-
generator.getSubstIndex(),
1225-
generator.getOrigFlags(),
1226-
generator.getOrigType(),
1227-
generator.getSubstParams());
1228-
} else {
1229-
handleScalar(generator.getOrigIndex(),
1230-
generator.getSubstIndex(),
1231-
generator.getOrigFlags(),
1232-
generator.getOrigType(),
1233-
generator.getSubstParams()[0]);
1234-
}
1210+
function(generator);
12351211
}
12361212
generator.finish();
12371213
}
@@ -2242,21 +2218,20 @@ class SubstFunctionTypePatternVisitor
22422218
};
22432219

22442220
pattern.forEachFunctionParam(func.getParams(), /*ignore self*/ false,
2245-
[&](unsigned origParamIndex, unsigned substParamIndex,
2246-
ParameterTypeFlags origFlags, AbstractionPattern origParamType,
2247-
AnyFunctionType::CanParam substParam) {
2248-
auto newParamTy = visit(substParam.getParameterType(), origParamType);
2249-
addParam(origFlags, newParamTy);
2250-
}, [&](unsigned origParamIndex, unsigned substParamIndex,
2251-
ParameterTypeFlags origFlags,
2252-
AbstractionPattern origExpansionType,
2253-
AnyFunctionType::CanParamArrayRef substParams) {
2254-
CanType candidateSubstType;
2255-
if (!substParams.empty())
2256-
candidateSubstType = substParams[0].getParameterType();
2257-
auto expansionType =
2258-
handlePackExpansion(origExpansionType, candidateSubstType);
2259-
addParam(origFlags, expansionType);
2221+
[&](FunctionParamGenerator &param) {
2222+
if (!param.isPackExpansion()) {
2223+
auto newParamTy = visit(param.getSubstParams()[0].getParameterType(),
2224+
param.getOrigType());
2225+
addParam(param.getOrigFlags(), newParamTy);
2226+
} else {
2227+
auto substParams = param.getSubstParams();
2228+
CanType candidateSubstType;
2229+
if (!substParams.empty())
2230+
candidateSubstType = substParams[0].getParameterType();
2231+
auto expansionType =
2232+
handlePackExpansion(param.getOrigType(), candidateSubstType);
2233+
addParam(param.getOrigFlags(), expansionType);
2234+
}
22602235
});
22612236

22622237
if (yieldType) {

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "swift/ClangImporter/ClangImporter.h"
3333
#include "swift/SIL/SILModule.h"
3434
#include "swift/SIL/SILType.h"
35+
#include "swift/SIL/AbstractionPatternGenerators.h"
3536
#include "clang/AST/ASTContext.h"
3637
#include "clang/AST/Attr.h"
3738
#include "clang/AST/DeclCXX.h"
@@ -1571,21 +1572,20 @@ class DestructureInputs {
15711572
// Process all the non-self parameters.
15721573
origType.forEachFunctionParam(params.drop_back(hasSelf ? 1 : 0),
15731574
/*ignore final orig param*/ hasSelf,
1574-
[&](unsigned origParamIndex, unsigned substParamIndex,
1575-
ParameterTypeFlags origFlags,
1576-
AbstractionPattern origParamType,
1577-
AnyFunctionType::CanParam substParam) {
1575+
[&](FunctionParamGenerator &param) {
15781576
// If the parameter is not a pack expansion, just pull off the
15791577
// next parameter and destructure it in parallel with the abstraction
15801578
// pattern for the type.
1581-
visit(origParamType, substParam, /*forSelf*/false);
1582-
}, [&](unsigned origParamIndex, unsigned substParamIndex,
1583-
ParameterTypeFlags origFlags,
1584-
AbstractionPattern origExpansionType,
1585-
AnyFunctionType::CanParamArrayRef substParams) {
1579+
if (!param.isPackExpansion()) {
1580+
visit(param.getOrigType(), param.getSubstParams()[0],
1581+
/*forSelf*/false);
1582+
return;
1583+
}
1584+
15861585
// Otherwise, collect the substituted components into a pack.
1586+
auto origExpansionType = param.getOrigType();
15871587
SmallVector<CanType, 8> packElts;
1588-
for (auto substParam : substParams) {
1588+
for (auto substParam : param.getSubstParams()) {
15891589
auto substParamType = substParam.getParameterType();
15901590
auto origParamType =
15911591
origExpansionType.getPackExpansionComponentType(substParamType);
@@ -1598,6 +1598,7 @@ class DestructureInputs {
15981598
SILPackType::ExtInfo extInfo(/*address*/ indirect);
15991599
auto packTy = SILPackType::get(TC.Context, extInfo, packElts);
16001600

1601+
auto origFlags = param.getOrigFlags();
16011602
addPackParameter(packTy, origFlags.getValueOwnership(),
16021603
origFlags.isNoDerivative());
16031604
});

0 commit comments

Comments
 (0)