Skip to content

Commit f94cb5b

Browse files
authored
Merge pull request swiftlang#5770 from gottesmm/refactor_computeresultplan_into_its_own_function
[gardening] Refactor out construction of the ResultPlan into a helper…
2 parents ea78313 + f360bfd commit f94cb5b

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,52 @@ static bool hasUnownedInnerPointerResult(CanSILFunctionType fnType) {
22722272
return false;
22732273
}
22742274

2275+
static ResultPlanPtr
2276+
computeResultPlan(SILGenFunction *SGF, CanSILFunctionType substFnType,
2277+
AbstractionPattern origResultType, CanType substResultType,
2278+
const Optional<ForeignErrorConvention> &foreignError,
2279+
SILFunctionTypeRepresentation rep, SILLocation loc,
2280+
SGFContext evalContext,
2281+
SmallVectorImpl<SILValue> &indirectResultAddrs) {
2282+
auto origResultTypeForPlan = origResultType;
2283+
auto substResultTypeForPlan = substResultType;
2284+
ArrayRef<SILResultInfo> allResults = substFnType->getAllResults();
2285+
SILResultInfo optResult;
2286+
2287+
// The plan needs to be built using the formal result type
2288+
// after foreign-error adjustment.
2289+
if (foreignError) {
2290+
switch (foreignError->getKind()) {
2291+
// These conventions make the formal result type ().
2292+
case ForeignErrorConvention::ZeroResult:
2293+
case ForeignErrorConvention::NonZeroResult:
2294+
assert(substResultType->isVoid());
2295+
allResults = {};
2296+
break;
2297+
2298+
// These conventions leave the formal result alone.
2299+
case ForeignErrorConvention::ZeroPreservedResult:
2300+
case ForeignErrorConvention::NonNilError:
2301+
break;
2302+
2303+
// This convention changes the formal result to the optional object
2304+
// type; we need to make our own make SILResultInfo array.
2305+
case ForeignErrorConvention::NilResult: {
2306+
assert(allResults.size() == 1);
2307+
SILType objectType =
2308+
allResults[0].getSILType().getAnyOptionalObjectType();
2309+
optResult = allResults[0].getWithType(objectType.getSwiftRValueType());
2310+
allResults = optResult;
2311+
break;
2312+
}
2313+
}
2314+
}
2315+
2316+
ResultPlanBuilder builder(*SGF, loc, allResults, rep, indirectResultAddrs);
2317+
return builder.build(evalContext.getEmitInto(), origResultTypeForPlan,
2318+
substResultTypeForPlan);
2319+
}
2320+
22752321
/// Emit a function application, assuming that the arguments have been
22762322
/// lowered appropriately for the abstraction level but that the
22772323
/// result does need to be turned back into something matching a
@@ -2292,45 +2338,9 @@ RValue SILGenFunction::emitApply(
22922338

22932339
// Create the result plan.
22942340
SmallVector<SILValue, 4> indirectResultAddrs;
2295-
ResultPlanPtr resultPlan = [&]() -> ResultPlanPtr {
2296-
auto origResultTypeForPlan = origResultType;
2297-
auto substResultTypeForPlan = substResultType;
2298-
ArrayRef<SILResultInfo> allResults = substFnType->getAllResults();
2299-
SILResultInfo optResult;
2300-
2301-
// The plan needs to be built using the formal result type
2302-
// after foreign-error adjustment.
2303-
if (foreignError) {
2304-
switch (foreignError->getKind()) {
2305-
// These conventions make the formal result type ().
2306-
case ForeignErrorConvention::ZeroResult:
2307-
case ForeignErrorConvention::NonZeroResult:
2308-
assert(substResultType->isVoid());
2309-
allResults = {};
2310-
break;
2311-
2312-
// These conventions leave the formal result alone.
2313-
case ForeignErrorConvention::ZeroPreservedResult:
2314-
case ForeignErrorConvention::NonNilError:
2315-
break;
2316-
2317-
// This convention changes the formal result to the optional object
2318-
// type; we need to make our own make SILResultInfo array.
2319-
case ForeignErrorConvention::NilResult: {
2320-
assert(allResults.size() == 1);
2321-
SILType objectType =
2322-
allResults[0].getSILType().getAnyOptionalObjectType();
2323-
optResult = allResults[0].getWithType(objectType.getSwiftRValueType());
2324-
allResults = optResult;
2325-
break;
2326-
}
2327-
}
2328-
}
2329-
2330-
ResultPlanBuilder builder(*this, loc, allResults, rep, indirectResultAddrs);
2331-
return builder.build(evalContext.getEmitInto(),
2332-
origResultTypeForPlan, substResultTypeForPlan);
2333-
}();
2341+
ResultPlanPtr resultPlan = computeResultPlan(
2342+
this, substFnType, origResultType, substResultType, foreignError, rep,
2343+
loc, evalContext, indirectResultAddrs);
23342344

23352345
// If the function returns an inner pointer, we'll need to lifetime-extend
23362346
// the 'self' parameter.

0 commit comments

Comments
 (0)