Skip to content

[CoroutineAccessors] Initial framing. #76526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/ABI/Mangling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,13 @@ Entities
ACCESSOR ::= 'G' // global getter
ACCESSOR ::= 'w' // willSet
ACCESSOR ::= 'W' // didSet
ACCESSOR ::= 'r' // read
ACCESSOR ::= 'M' // modify (temporary)
ACCESSOR ::= 'r' // _read
ACCESSOR ::= 'M' // _modify (temporary)
ACCESSOR ::= 'a' ADDRESSOR-KIND // mutable addressor
ACCESSOR ::= 'l' ADDRESSOR-KIND // non-mutable addressor
ACCESSOR ::= 'p' // pseudo accessor referring to the storage itself
ACCESSOR ::= 'x' // modify
ACCESSOR ::= 'y' // read

ADDRESSOR-KIND ::= 'u' // unsafe addressor (no owner)
ADDRESSOR-KIND ::= 'O' // owning addressor (non-native owner), not used anymore
Expand Down
4 changes: 4 additions & 0 deletions include/swift/ABI/MetadataValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ class MethodDescriptorFlags {
Setter,
ModifyCoroutine,
ReadCoroutine,
Read2Coroutine,
Modify2Coroutine,
};

private:
Expand Down Expand Up @@ -594,6 +596,8 @@ class ProtocolRequirementFlags {
ModifyCoroutine,
AssociatedTypeAccessFunction,
AssociatedConformanceAccessFunction,
Read2Coroutine,
Modify2Coroutine,
};

private:
Expand Down
30 changes: 25 additions & 5 deletions include/swift/AST/AccessorKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@
#define COROUTINE_ACCESSOR(ID, KEYWORD) OPAQUE_ACCESSOR(ID, KEYWORD)
#endif

/// EXPERIMENTAL_COROUTINE_ACCESSOR(ID, KEYWORD, FEATURE)
/// The given coroutine accessor is experimental.
///
/// Defaults to COROUTINE_ACCESSOR(ID, KEYWORD)
#ifndef EXPERIMENTAL_COROUTINE_ACCESSOR
#define EXPERIMENTAL_COROUTINE_ACCESSOR(ID, KEYWORD, FEATURE) COROUTINE_ACCESSOR(ID, KEYWORD)
#endif

/// ANY_ADDRESSOR(ID, KEYWORD)
/// The given keyword corresponds to an addressor of the given kind.
///
Expand Down Expand Up @@ -108,11 +116,6 @@
#define INIT_ACCESSOR(ID, KEYWORD) SINGLETON_ACCESSOR(ID, KEYWORD)
#endif

// Suppress entries for accessors which can't be written in source code.
#ifndef SUPPRESS_ARTIFICIAL_ACCESSORS
#define SUPPRESS_ARTIFICIAL_ACCESSORS 0
#endif

/// This is a getter: a function that is called when a value is loaded
/// from the storage. It returns an owned value of the storage type.
///
Expand Down Expand Up @@ -150,6 +153,14 @@ OBJC_ACCESSOR(Set, set)
/// one can always be synthesized (even if the storage type is move-only).
COROUTINE_ACCESSOR(Read, _read)

/// This is a read accessor: a yield-once coroutine which is called when a
/// value is loaded from the storage, like a getter, but which works
/// by yielding a borrowed value of the storage type.
///
/// If the storage is not implemented with a read accessor then
/// one can always be synthesized (even if the storage type is move-only).
EXPERIMENTAL_COROUTINE_ACCESSOR(Read2, read, CoroutineAccessors)

/// This is a modify accessor: a yield-once coroutine which is called when a
/// the storage is modified which works by yielding an inout value
/// of the storage type.
Expand All @@ -158,6 +169,14 @@ COROUTINE_ACCESSOR(Read, _read)
/// one can be synthesized if the storage is mutable at all.
COROUTINE_ACCESSOR(Modify, _modify)

/// This is a modify accessor: a yield-once coroutine which is called when a
/// the storage is modified which works by yielding an inout value
/// of the storage type.
///
/// If the storage is not implemented with a modify accessor then
/// one can be synthesized if the storage is mutable at all.
EXPERIMENTAL_COROUTINE_ACCESSOR(Modify2, modify, CoroutineAccessors)

/// This is a willSet observer: a function which "decorates" an
/// underlying assignment operation by being called prior to the
/// operation when a value is assigned to the storage.
Expand Down Expand Up @@ -210,6 +229,7 @@ LAST_ACCESSOR(Init)
#undef OBJC_ACCESSOR
#undef OPAQUE_ACCESSOR
#undef COROUTINE_ACCESSOR
#undef EXPERIMENTAL_COROUTINE_ACCESSOR
#undef OBSERVING_ACCESSOR
#undef SINGLETON_ACCESSOR
#undef ACCESSOR
Expand Down
8 changes: 4 additions & 4 deletions include/swift/AST/AnyFunctionRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,10 @@ class AnyFunctionRef {
->getReferenceStorageReferent();
if (mapIntoContext)
valueTy = AD->mapTypeIntoContext(valueTy);
YieldTypeFlags flags(AD->getAccessorKind() == AccessorKind::Modify
? ParamSpecifier::InOut
: ParamSpecifier::LegacyShared);
YieldTypeFlags flags(
isYieldingDefaultMutatingAccessor(AD->getAccessorKind())
? ParamSpecifier::InOut
: ParamSpecifier::LegacyShared);
buffer.push_back(AnyFunctionType::Yield(valueTy, flags));
return buffer;
}
Expand Down Expand Up @@ -304,4 +305,3 @@ struct DenseMapInfo<swift::AnyFunctionRef> {
}

#endif // LLVM_SWIFT_AST_ANY_FUNCTION_REF_H

3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ ERROR(observing_accessor_in_subscript,none,
"%select{'willSet'|'didSet'}0 is not allowed in subscripts", (unsigned))
ERROR(getset_cannot_be_implied,none,
"variable with implied type cannot have implied getter/setter", ())
ERROR(accessor_requires_coroutine_accessors,none,
"%0 is only valid when experimental feature coroutine accessors is enabled",
(StringRef))

// Import
ERROR(decl_expected_module_name,none,
Expand Down
11 changes: 6 additions & 5 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1710,9 +1710,10 @@ ERROR(functions_mutating_and_not,none,
ERROR(static_functions_not_mutating,none,
"static functions must not be declared mutating", ())

ERROR(modify_mutatingness_differs_from_setter,none,
"'modify' accessor cannot be %0 when the setter is %1",
(SelfAccessKind, SelfAccessKind))
ERROR(readwriter_mutatingness_differs_from_reader_or_writer_mutatingness,none,
"%0 cannot be %1 when "
"%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",
(StringRef, SelfAccessKind, unsigned, StringRef, SelfAccessKind, StringRef, SelfAccessKind))

ERROR(transparent_in_protocols_not_supported,none,
"'@_transparent' attribute is not supported on declarations within protocols", ())
Expand Down Expand Up @@ -6584,8 +6585,8 @@ ERROR(dynamic_with_transparent,none,
ERROR(dynamic_replacement_accessor_not_dynamic, none,
"replaced accessor for %0 is not marked dynamic", (DeclName))
ERROR(dynamic_replacement_accessor_not_explicit, none,
"replaced accessor %select{get|set|_read|_modify|willSet|didSet|unsafeAddress|addressWithOwner|addressWithNativeOwner|unsafeMutableAddress|mutableAddressWithOwner|}0 for %1 is not explicitly defined",
(unsigned, DeclName))
"replaced %0 for %1 is not explicitly defined",
(StringRef, DeclName))
ERROR(dynamic_replacement_function_not_dynamic, none,
"replaced function %0 is not marked dynamic", (DeclName))
ERROR(dynamic_replacement_function_not_found, none,
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ struct PrintOptions {
/// Suppress ~Escapable types and lifetime dependence annotations
bool SuppressNonEscapableTypes = false;

/// Suppress modify/read accessors.
bool SuppressCoroutineAccessors = false;

/// List of attribute kinds that should not be printed.
std::vector<AnyAttrKind> ExcludeAttrList = {
DeclAttrKind::Transparent, DeclAttrKind::Effects,
Expand Down
Loading