Skip to content

Commit ba8f8ea

Browse files
Merge pull request #76526 from nate-chandler/general-coro/20240906/1
[CoroutineAccessors] Initial framing.
2 parents f563de5 + 091368b commit ba8f8ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1850
-124
lines changed

docs/ABI/Mangling.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,13 @@ Entities
387387
ACCESSOR ::= 'G' // global getter
388388
ACCESSOR ::= 'w' // willSet
389389
ACCESSOR ::= 'W' // didSet
390-
ACCESSOR ::= 'r' // read
391-
ACCESSOR ::= 'M' // modify (temporary)
390+
ACCESSOR ::= 'r' // _read
391+
ACCESSOR ::= 'M' // _modify (temporary)
392392
ACCESSOR ::= 'a' ADDRESSOR-KIND // mutable addressor
393393
ACCESSOR ::= 'l' ADDRESSOR-KIND // non-mutable addressor
394394
ACCESSOR ::= 'p' // pseudo accessor referring to the storage itself
395+
ACCESSOR ::= 'x' // modify
396+
ACCESSOR ::= 'y' // read
395397

396398
ADDRESSOR-KIND ::= 'u' // unsafe addressor (no owner)
397399
ADDRESSOR-KIND ::= 'O' // owning addressor (non-native owner), not used anymore

include/swift/ABI/MetadataValues.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ class MethodDescriptorFlags {
372372
Setter,
373373
ModifyCoroutine,
374374
ReadCoroutine,
375+
Read2Coroutine,
376+
Modify2Coroutine,
375377
};
376378

377379
private:
@@ -594,6 +596,8 @@ class ProtocolRequirementFlags {
594596
ModifyCoroutine,
595597
AssociatedTypeAccessFunction,
596598
AssociatedConformanceAccessFunction,
599+
Read2Coroutine,
600+
Modify2Coroutine,
597601
};
598602

599603
private:

include/swift/AST/AccessorKinds.def

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@
7373
#define COROUTINE_ACCESSOR(ID, KEYWORD) OPAQUE_ACCESSOR(ID, KEYWORD)
7474
#endif
7575

76+
/// EXPERIMENTAL_COROUTINE_ACCESSOR(ID, KEYWORD, FEATURE)
77+
/// The given coroutine accessor is experimental.
78+
///
79+
/// Defaults to COROUTINE_ACCESSOR(ID, KEYWORD)
80+
#ifndef EXPERIMENTAL_COROUTINE_ACCESSOR
81+
#define EXPERIMENTAL_COROUTINE_ACCESSOR(ID, KEYWORD, FEATURE) COROUTINE_ACCESSOR(ID, KEYWORD)
82+
#endif
83+
7684
/// ANY_ADDRESSOR(ID, KEYWORD)
7785
/// The given keyword corresponds to an addressor of the given kind.
7886
///
@@ -108,11 +116,6 @@
108116
#define INIT_ACCESSOR(ID, KEYWORD) SINGLETON_ACCESSOR(ID, KEYWORD)
109117
#endif
110118

111-
// Suppress entries for accessors which can't be written in source code.
112-
#ifndef SUPPRESS_ARTIFICIAL_ACCESSORS
113-
#define SUPPRESS_ARTIFICIAL_ACCESSORS 0
114-
#endif
115-
116119
/// This is a getter: a function that is called when a value is loaded
117120
/// from the storage. It returns an owned value of the storage type.
118121
///
@@ -150,6 +153,14 @@ OBJC_ACCESSOR(Set, set)
150153
/// one can always be synthesized (even if the storage type is move-only).
151154
COROUTINE_ACCESSOR(Read, _read)
152155

156+
/// This is a read accessor: a yield-once coroutine which is called when a
157+
/// value is loaded from the storage, like a getter, but which works
158+
/// by yielding a borrowed value of the storage type.
159+
///
160+
/// If the storage is not implemented with a read accessor then
161+
/// one can always be synthesized (even if the storage type is move-only).
162+
EXPERIMENTAL_COROUTINE_ACCESSOR(Read2, read, CoroutineAccessors)
163+
153164
/// This is a modify accessor: a yield-once coroutine which is called when a
154165
/// the storage is modified which works by yielding an inout value
155166
/// of the storage type.
@@ -158,6 +169,14 @@ COROUTINE_ACCESSOR(Read, _read)
158169
/// one can be synthesized if the storage is mutable at all.
159170
COROUTINE_ACCESSOR(Modify, _modify)
160171

172+
/// This is a modify accessor: a yield-once coroutine which is called when a
173+
/// the storage is modified which works by yielding an inout value
174+
/// of the storage type.
175+
///
176+
/// If the storage is not implemented with a modify accessor then
177+
/// one can be synthesized if the storage is mutable at all.
178+
EXPERIMENTAL_COROUTINE_ACCESSOR(Modify2, modify, CoroutineAccessors)
179+
161180
/// This is a willSet observer: a function which "decorates" an
162181
/// underlying assignment operation by being called prior to the
163182
/// operation when a value is assigned to the storage.
@@ -210,6 +229,7 @@ LAST_ACCESSOR(Init)
210229
#undef OBJC_ACCESSOR
211230
#undef OPAQUE_ACCESSOR
212231
#undef COROUTINE_ACCESSOR
232+
#undef EXPERIMENTAL_COROUTINE_ACCESSOR
213233
#undef OBSERVING_ACCESSOR
214234
#undef SINGLETON_ACCESSOR
215235
#undef ACCESSOR

include/swift/AST/AnyFunctionRef.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,10 @@ class AnyFunctionRef {
260260
->getReferenceStorageReferent();
261261
if (mapIntoContext)
262262
valueTy = AD->mapTypeIntoContext(valueTy);
263-
YieldTypeFlags flags(AD->getAccessorKind() == AccessorKind::Modify
264-
? ParamSpecifier::InOut
265-
: ParamSpecifier::LegacyShared);
263+
YieldTypeFlags flags(
264+
isYieldingDefaultMutatingAccessor(AD->getAccessorKind())
265+
? ParamSpecifier::InOut
266+
: ParamSpecifier::LegacyShared);
266267
buffer.push_back(AnyFunctionType::Yield(valueTy, flags));
267268
return buffer;
268269
}
@@ -304,4 +305,3 @@ struct DenseMapInfo<swift::AnyFunctionRef> {
304305
}
305306

306307
#endif // LLVM_SWIFT_AST_ANY_FUNCTION_REF_H
307-

include/swift/AST/DiagnosticsParse.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ ERROR(observing_accessor_in_subscript,none,
305305
"%select{'willSet'|'didSet'}0 is not allowed in subscripts", (unsigned))
306306
ERROR(getset_cannot_be_implied,none,
307307
"variable with implied type cannot have implied getter/setter", ())
308+
ERROR(accessor_requires_coroutine_accessors,none,
309+
"%0 is only valid when experimental feature coroutine accessors is enabled",
310+
(StringRef))
308311

309312
// Import
310313
ERROR(decl_expected_module_name,none,

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,9 +1710,10 @@ ERROR(functions_mutating_and_not,none,
17101710
ERROR(static_functions_not_mutating,none,
17111711
"static functions must not be declared mutating", ())
17121712

1713-
ERROR(modify_mutatingness_differs_from_setter,none,
1714-
"'modify' accessor cannot be %0 when the setter is %1",
1715-
(SelfAccessKind, SelfAccessKind))
1713+
ERROR(readwriter_mutatingness_differs_from_reader_or_writer_mutatingness,none,
1714+
"%0 cannot be %1 when "
1715+
"%select{both the %3 is %4 and the %5 is not %6|either the %3 is not %4 or the %5 is %6|the %3 is not %4|the %5 is %6}2",
1716+
(StringRef, SelfAccessKind, unsigned, StringRef, SelfAccessKind, StringRef, SelfAccessKind))
17161717

17171718
ERROR(transparent_in_protocols_not_supported,none,
17181719
"'@_transparent' attribute is not supported on declarations within protocols", ())
@@ -6584,8 +6585,8 @@ ERROR(dynamic_with_transparent,none,
65846585
ERROR(dynamic_replacement_accessor_not_dynamic, none,
65856586
"replaced accessor for %0 is not marked dynamic", (DeclName))
65866587
ERROR(dynamic_replacement_accessor_not_explicit, none,
6587-
"replaced accessor %select{get|set|_read|_modify|willSet|didSet|unsafeAddress|addressWithOwner|addressWithNativeOwner|unsafeMutableAddress|mutableAddressWithOwner|}0 for %1 is not explicitly defined",
6588-
(unsigned, DeclName))
6588+
"replaced %0 for %1 is not explicitly defined",
6589+
(StringRef, DeclName))
65896590
ERROR(dynamic_replacement_function_not_dynamic, none,
65906591
"replaced function %0 is not marked dynamic", (DeclName))
65916592
ERROR(dynamic_replacement_function_not_found, none,

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ struct PrintOptions {
390390
/// Suppress ~Escapable types and lifetime dependence annotations
391391
bool SuppressNonEscapableTypes = false;
392392

393+
/// Suppress modify/read accessors.
394+
bool SuppressCoroutineAccessors = false;
395+
393396
/// List of attribute kinds that should not be printed.
394397
std::vector<AnyAttrKind> ExcludeAttrList = {
395398
DeclAttrKind::Transparent, DeclAttrKind::Effects,

0 commit comments

Comments
 (0)