Skip to content

Commit 71f994d

Browse files
authored
Merge pull request #79372 from drexin/wip-refactor-typed-throws
[IRGen] Remove code duplication in CallEmission::emitToUnmappedExplosionWithDirectTypedError
2 parents 5a1ad8a + 40cdd36 commit 71f994d

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4532,23 +4532,30 @@ void CallEmission::emitToUnmappedExplosionWithDirectTypedError(
45324532
extractScalarResults(IGF, result->getType(), result, nativeExplosion);
45334533
auto values = nativeExplosion.claimAll();
45344534

4535-
Explosion errorExplosion;
4536-
if (!errorSchema.empty()) {
4537-
auto *expandedType = errorSchema.getExpandedType(IGF.IGM);
4535+
auto convertIntoExplosion = [](IRGenFunction &IGF,
4536+
const NativeConventionSchema &schema,
4537+
llvm::ArrayRef<llvm::Value *> values,
4538+
Explosion &explosion,
4539+
std::function<unsigned(unsigned)> mapIndex) {
4540+
auto *expandedType = schema.getExpandedType(IGF.IGM);
45384541
if (auto *structTy = dyn_cast<llvm::StructType>(expandedType)) {
45394542
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
4540-
llvm::Value *elt = values[combined.errorValueMapping[i]];
4543+
llvm::Value *elt = values[mapIndex(i)];
45414544
auto *nativeTy = structTy->getElementType(i);
45424545
elt = convertForDirectError(IGF, elt, nativeTy, /*forExtraction*/ true);
4543-
errorExplosion.add(elt);
4546+
explosion.add(elt);
45444547
}
45454548
} else {
4546-
auto *converted =
4547-
convertForDirectError(IGF, values[combined.errorValueMapping[0]],
4548-
expandedType, /*forExtraction*/ true);
4549-
errorExplosion.add(converted);
4549+
auto *converted = convertForDirectError(
4550+
IGF, values[mapIndex(0)], expandedType, /*forExtraction*/ true);
4551+
explosion.add(converted);
45504552
}
4553+
};
45514554

4555+
Explosion errorExplosion;
4556+
if (!errorSchema.empty()) {
4557+
convertIntoExplosion(IGF, errorSchema, values, errorExplosion,
4558+
[&](auto i) { return combined.errorValueMapping[i]; });
45524559
typedErrorExplosion =
45534560
errorSchema.mapFromNative(IGF.IGM, IGF, errorExplosion, errorType);
45544561
} else {
@@ -4558,19 +4565,8 @@ void CallEmission::emitToUnmappedExplosionWithDirectTypedError(
45584565
// If the regular result type is void, there is nothing to explode
45594566
if (!nativeSchema.empty()) {
45604567
Explosion resultExplosion;
4561-
auto *expandedType = nativeSchema.getExpandedType(IGF.IGM);
4562-
if (auto *structTy = dyn_cast<llvm::StructType>(expandedType)) {
4563-
for (unsigned i = 0, e = structTy->getNumElements(); i < e; ++i) {
4564-
auto *nativeTy = structTy->getElementType(i);
4565-
auto *converted = convertForDirectError(IGF, values[i], nativeTy,
4566-
/*forExtraction*/ true);
4567-
resultExplosion.add(converted);
4568-
}
4569-
} else {
4570-
auto *converted = convertForDirectError(IGF, values[0], expandedType,
4571-
/*forExtraction*/ true);
4572-
resultExplosion.add(converted);
4573-
}
4568+
convertIntoExplosion(IGF, nativeSchema, values, resultExplosion,
4569+
[](auto i) { return i; });
45744570
out = nativeSchema.mapFromNative(IGF.IGM, IGF, resultExplosion, resultType);
45754571
}
45764572
}

0 commit comments

Comments
 (0)