Implement generalized accessors using yield-once coroutines.

For now, the accessors have been underscored as `_read` and `_modify`.
I'll prepare an evolution proposal for this feature which should allow
us to remove the underscores or, y'know, rename them to `purple` and
`lettuce`.

`_read` accessors do not make any effort yet to avoid copying the
value being yielded.  I'll work on it in follow-up patches.

Opaque accesses to properties and subscripts defined with `_modify`
accessors will use an inefficient `materializeForSet` pattern that
materializes the value to a temporary instead of accessing it in-place.
That will be fixed by migrating to `modify` over `materializeForSet`,
which is next up after the `read` optimizations.

SIL ownership verification doesn't pass yet for the test cases here
because of a general fault in SILGen where borrows can outlive their
borrowed value due to being cleaned up on the general cleanup stack
when the borrowed value is cleaned up on the formal-access stack.
Michael, Andy, and I discussed various ways to fix this, but it seems
clear to me that it's not in any way specific to coroutine accesses.

rdar://35399664
This commit is contained in:
John McCall
2018-07-22 02:28:59 -04:00
parent 46a83909c6
commit 7a4aeed570
91 changed files with 1935 additions and 136 deletions

View File

@@ -835,6 +835,16 @@ static void synthesizeAddressedGetterBody(TypeChecker &TC,
synthesizeTrivialGetterBody(TC, getter, TargetImpl::Implementation);
}
/// Synthesize the body of a getter which just delegates to a read
/// coroutine accessor.
static void synthesizeReadCoroutineGetterBody(TypeChecker &TC,
AccessorDecl *getter) {
assert(getter->getStorage()->getReadCoroutine());
// This should call the read coroutine.
synthesizeTrivialGetterBody(TC, getter, TargetImpl::Implementation);
}
/// Synthesize the body of a setter which just stores to the given storage
/// declaration (which doesn't have to be the storage for the setter).
static void synthesizeTrivialSetterBodyWithStorage(TypeChecker &TC,
@@ -905,8 +915,20 @@ static void convertStoredVarInProtocolToComputed(VarDecl *VD, TypeChecker &TC) {
VD->overwriteImplInfo(StorageImplInfo::getImmutableComputed());
}
/// Synthesize the body of a setter which just delegates to a mutable
/// addressor.
static void synthesizeMutableAddressSetterBody(TypeChecker &TC,
AccessorDecl *setter) {
// This should call the mutable addressor.
synthesizeTrivialSetterBodyWithStorage(TC, setter, TargetImpl::Implementation,
setter->getStorage());
}
/// Synthesize the body of a setter which just delegates to a modify
/// coroutine accessor.
static void synthesizeModifyCoroutineSetterBody(TypeChecker &TC,
AccessorDecl *setter) {
// This should call the modify coroutine.
synthesizeTrivialSetterBodyWithStorage(TC, setter, TargetImpl::Implementation,
setter->getStorage());
}
@@ -1756,6 +1778,7 @@ void swift::triggerAccessorSynthesis(TypeChecker &TC,
case ReadImplKind::Stored:
case ReadImplKind::Inherited:
case ReadImplKind::Address:
case ReadImplKind::Read:
if (auto getter = storage->getGetter())
triggerSynthesis(TC, getter, SynthesizedFunction::Getter);
break;
@@ -1771,6 +1794,7 @@ void swift::triggerAccessorSynthesis(TypeChecker &TC,
case WriteImplKind::StoredWithObservers:
case WriteImplKind::InheritedWithObservers:
case WriteImplKind::MutableAddress:
case WriteImplKind::Modify:
if (auto setter = storage->getSetter())
triggerSynthesis(TC, setter, SynthesizedFunction::Setter);
break;
@@ -2036,6 +2060,10 @@ static void synthesizeGetterBody(TypeChecker &TC, AccessorDecl *getter) {
case ReadImplKind::Address:
synthesizeAddressedGetterBody(TC, getter);
return;
case ReadImplKind::Read:
synthesizeReadCoroutineGetterBody(TC, getter);
return;
}
llvm_unreachable("bad ReadImplKind");
}
@@ -2059,6 +2087,10 @@ static void synthesizeSetterBody(TypeChecker &TC, AccessorDecl *setter) {
case WriteImplKind::MutableAddress:
return synthesizeMutableAddressSetterBody(TC, setter);
case WriteImplKind::Modify:
synthesizeModifyCoroutineSetterBody(TC, setter);
return;
}
llvm_unreachable("bad ReadImplKind");
}