Skip to content

Commit cc13060

Browse files
committed
[SILGenBuilder] NFC: Add createUncheckedForwardingCast that works with ManagedValue
1 parent 5e08f7e commit cc13060

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/SILGen/SILGenBuilder.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,31 @@ ManagedValue SILGenBuilder::createUncheckedBitCast(SILLocation loc,
757757
return cloner.clone(cast);
758758
}
759759

760+
ManagedValue SILGenBuilder::createUncheckedForwardingCast(SILLocation loc,
761+
ManagedValue value,
762+
SILType type) {
763+
CleanupCloner cloner(*this, value);
764+
SILValue cast = createUncheckedForwardingCast(loc, value.getValue(), type);
765+
766+
// Currently createUncheckedBitCast only produces these
767+
// instructions. We assert here to make sure if this changes, this code is
768+
// updated.
769+
assert((isa<UncheckedTrivialBitCastInst>(cast) ||
770+
isa<UncheckedRefCastInst>(cast) ||
771+
isa<UncheckedValueCastInst>(cast) ||
772+
isa<ConvertFunctionInst>(cast)) &&
773+
"SILGenBuilder is out of sync with SILBuilder.");
774+
775+
// If we have a trivial inst, just return early.
776+
if (isa<UncheckedTrivialBitCastInst>(cast))
777+
return ManagedValue::forObjectRValueWithoutOwnership(cast);
778+
779+
// Otherwise, we forward the cleanup of the input value and place the cleanup
780+
// on the cast value since unchecked_ref_cast is "forwarding".
781+
value.forward(SGF);
782+
return cloner.clone(cast);
783+
}
784+
760785
ManagedValue SILGenBuilder::createOpenExistentialRef(SILLocation loc,
761786
ManagedValue original,
762787
SILType type) {

lib/SILGen/SILGenBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ class SILGenBuilder : public SILBuilder {
340340
ManagedValue createUncheckedBitCast(SILLocation loc, ManagedValue original,
341341
SILType type);
342342

343+
using SILBuilder::createUncheckedForwardingCast;
344+
ManagedValue createUncheckedForwardingCast(SILLocation loc,
345+
ManagedValue original,
346+
SILType type);
347+
343348
using SILBuilder::createOpenExistentialRef;
344349
ManagedValue createOpenExistentialRef(SILLocation loc, ManagedValue arg,
345350
SILType openedType);

0 commit comments

Comments
 (0)