Skip to content

Commit 3985a11

Browse files
committed
[semantic-sil] Add a new API ManagedValue::transform() for transforming/forwarding a cleanup for a ManagedValue.
In SILGen, we often times want to transform a ManagedValue, while maintaining the underlying cleanup. The ManagedValue::transform API attempts to formalize this pattern through the usage of std::move to create a movable rvalue, but returning a different ManagedValue as a full value. This ensure that the original ManagedValue is destroyed at end of statement, will allowing the underlying cleanup to be forwarded. From an ownership perspective, I think the majority of these cases should actually recreate the underlying cleanup since in most cases they involve forwarding. For today though I am using this to just formalize this pattern at the relevant call sites. rdar://29791263
1 parent 741001d commit 3985a11

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/SILGen/ManagedValue.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,22 @@ class ManagedValue {
123123
SILValue getValue() const { return valueAndFlag.getPointer(); }
124124

125125
SILType getType() const { return getValue()->getType(); }
126-
126+
127+
/// Transform the given ManagedValue, replacing the underlying value, but
128+
/// keeping the same cleanup.
129+
///
130+
/// For owned values, this is equivalent to forwarding the cleanup and
131+
/// creating a new cleanup of the same type on the new value. This is useful
132+
/// for forwarding sequences.
133+
///
134+
/// For all other values, it is a move.
135+
ManagedValue transform(SILValue newValue) && {
136+
assert(getValue().getOwnershipKind() == newValue.getOwnershipKind() &&
137+
"New value and old value must have the same ownership kind");
138+
ManagedValue M(newValue, isLValue(), getCleanup());
139+
*this = ManagedValue();
140+
return M;
141+
}
127142

128143
CanType getSwiftType() const {
129144
return isLValue()

0 commit comments

Comments
 (0)