Skip to content

Commit e97fde0

Browse files
authored
Merge pull request #17212 from rjmccall/generalize-accessor-kinds
Rename accessor kinds from IsGetter -> IsGet, etc.
2 parents 85275d2 + 9022b51 commit e97fde0

38 files changed

+748
-607
lines changed

include/swift/AST/AccessorKinds.def

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
//===--- AccessorKinds.def - Swift accessor metaprogramming -----*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines macros used for macro-metaprogramming with accessor kinds.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#if defined(ACCESSOR) && defined(ACCESSOR_KEYWORD)
18+
#error do not define both ACCESSOR and ACCESSOR_KEYWORD
19+
#elif !defined(ACCESSOR) && !defined(ACCESSOR_KEYWORD)
20+
#error must define either ACCESSOR or ACCESSOR_KEYWORD
21+
#endif
22+
23+
/// ACCESSOR(ID)
24+
/// There is an accessor with the enumerator value AccessorKind::ID.
25+
#if !defined(ACCESSOR) && defined(ACCESSOR_KEYWORD)
26+
#define ACCESSOR(ID)
27+
#endif
28+
29+
/// SINGLETON_ACCESSOR(ID, KEYWORD)
30+
/// The given accessor has only one matching accessor keyword.
31+
///
32+
/// Defaults to ACCESSOR(ID) or ACCESSOR_KEYWORD(KEYWORD), depending on which
33+
/// is defined.
34+
#ifndef SINGLETON_ACCESSOR
35+
#if defined(ACCESSOR_KEYWORD)
36+
#define SINGLETON_ACCESSOR(ID, KEYWORD) ACCESSOR_KEYWORD(KEYWORD)
37+
#else
38+
#define SINGLETON_ACCESSOR(ID, KEYWORD) ACCESSOR(ID)
39+
#endif
40+
#endif
41+
42+
/// OBSERVING_ACCESSOR(ID, KEYWORD)
43+
/// The given accessor is an observing accessor.
44+
///
45+
/// Defaults to SINGLETON_ACCESSOR(ID, KEYWORD).
46+
#ifndef OBSERVING_ACCESSOR
47+
#define OBSERVING_ACCESSOR(ID, KEYWORD) SINGLETON_ACCESSOR(ID, KEYWORD)
48+
#endif
49+
50+
/// OPAQUE_ACCESSOR(ID, KEYWORD)
51+
/// The given accessor is used in the opaque access pattern and is created
52+
/// for (mutable) storage whenever its implementation is unknown, such as
53+
/// when it is resilient, overridable, or accessed through a protocol.
54+
///
55+
/// Defaults to SINGLETON_ACCESSOR(ID, KEYWORD).
56+
#ifndef OPAQUE_ACCESSOR
57+
#define OPAQUE_ACCESSOR(ID, KEYWORD) SINGLETON_ACCESSOR(ID, KEYWORD)
58+
#endif
59+
60+
/// OBJC_ACCESSOR(ID, KEYWORD)
61+
/// The given accessor is used in Objective-C, i.e. it is a getter or setter.
62+
///
63+
/// Defaults to OPAQUE_ACCESSOR(ID, KEYWORD).
64+
#ifndef OBJC_ACCESSOR
65+
#define OBJC_ACCESSOR(ID, KEYWORD) OPAQUE_ACCESSOR(ID, KEYWORD)
66+
#endif
67+
68+
/// ANY_ADDRESSOR(ACCESSOR_ID, ADDRESSOR_ID, KEYWORD)
69+
/// The given keyword corresponds to an addressor of the given kind.
70+
///
71+
/// Defaults to ACCESSOR_KEYWORD(KEYWORD) if that is defined; otherwise
72+
/// defaults to nothing.
73+
#ifndef ANY_ADDRESSOR
74+
#if defined(ACCESSOR_KEYWORD)
75+
#define ANY_ADDRESSOR(ACCESSOR_ID, ADDRESSOR_ID, KEYWORD) \
76+
ACCESSOR_KEYWORD(KEYWORD)
77+
#else
78+
#define ANY_ADDRESSOR(ACCESSOR_ID, ADDRESSOR_ID, KEYWORD)
79+
#endif
80+
#endif
81+
82+
/// IMMUTABLE_ADDRESSOR(ADDRESSOR_ID, KEYWORD)
83+
/// The given keyword corresponds to an immutable addressor of the given kind.
84+
///
85+
/// DEfaults to ANY_ADDRESSOR(Address, ADDRESSOR_ID, KEYWORD).
86+
#ifndef IMMUTABLE_ADDRESSOR
87+
#define IMMUTABLE_ADDRESSOR(ADDRESSOR_ID, KEYWORD) \
88+
ANY_ADDRESSOR(Address, ADDRESSOR_ID, KEYWORD)
89+
#endif
90+
91+
/// MUTABLE_ADDRESSOR(ADDRESSOR_ID, KEYWORD)
92+
/// The given keyword corresponds to a mutable addressor of the given kind.
93+
///
94+
/// DEfaults to ANY_ADDRESSOR(MutableAddress, ADDRESSOR_ID, KEYWORD).
95+
#ifndef MUTABLE_ADDRESSOR
96+
#define MUTABLE_ADDRESSOR(ADDRESSOR_ID, KEYWORD) \
97+
ANY_ADDRESSOR(MutableAddress, ADDRESSOR_ID, KEYWORD)
98+
#endif
99+
100+
// Suppress entries for accessors which can't be written in source code.
101+
#ifndef SUPPRESS_ARTIFICIAL_ACCESSORS
102+
#define SUPPRESS_ARTIFICIAL_ACCESSORS 0
103+
#endif
104+
105+
/// This is a getter: a function that is called when a value is loaded
106+
/// from the storage. It returns an owned value of the storage type.
107+
///
108+
/// If the storage is not implemented with a getter, a getter can
109+
/// always be synthesized. However, this will not be true when
110+
/// move-only types are introduced.
111+
OBJC_ACCESSOR(Get, get)
112+
113+
/// This is a setter: a function that is called when a value is assigned
114+
/// to the storage. It takes a value of the storage type as its
115+
/// primary parameter, in addition to any index values that may be
116+
/// required for a subscript.
117+
///
118+
/// If the storage is not implemented with a setter, a setter can
119+
/// always be synthesized if the storage is mutable at all.
120+
OBJC_ACCESSOR(Set, set)
121+
122+
/// This is a materializeForSet accessor: a function which is called
123+
/// when a combined read-modify operation is performed on the storage,
124+
/// such as passing it as an inout argument (which includes calling
125+
/// a mutating function on it). It is essentially a SILGen-lowered
126+
/// coroutine that yields a pointer to a mutable value of the storage
127+
/// type.
128+
///
129+
/// We do not allow users to provide materializeForSet. It can always
130+
/// be synthesized if the storage is mutable at all.
131+
#if !(SUPPRESS_ARTIFICIAL_ACCESSORS)
132+
OPAQUE_ACCESSOR(MaterializeForSet, materializeForSet)
133+
#endif
134+
135+
/// This is a willSet observer: a function which "decorates" an
136+
/// underlying assignment operation by being called prior to the
137+
/// operation when a value is assigned to the storage.
138+
///
139+
/// willSet is essentially sugar for implementing a certain common
140+
/// setter idiom.
141+
OBSERVING_ACCESSOR(WillSet, willSet)
142+
143+
/// This is a didSet observer: a function which "decorates" an
144+
/// underlying assignment operation by being called after the
145+
/// operation when a value is assigned to the storage.
146+
///
147+
/// didSet is essentially sugar for implementing a certain common
148+
/// setter idiom.
149+
OBSERVING_ACCESSOR(DidSet, didSet)
150+
151+
/// This is an address-family accessor: a function that is called when
152+
/// a value is loaded from the storage, like a getter, but which works
153+
/// by returning a pointer to an immutable value of the storage type.
154+
/// This kind of accessor also has an addressor kind.
155+
///
156+
/// Addressors are a way of proving more efficient access to storage
157+
/// when it is already stored in memory (but not as a stored member
158+
/// of the type).
159+
ACCESSOR(Address)
160+
161+
IMMUTABLE_ADDRESSOR(Unsafe, unsafeAddress)
162+
IMMUTABLE_ADDRESSOR(Owning, addressWithOwner)
163+
IMMUTABLE_ADDRESSOR(NativeOwning, addressWithNativeOwner)
164+
IMMUTABLE_ADDRESSOR(NativePinning, addressWithPinnedNativeOwner)
165+
166+
/// This is a mutableAddress-family accessor: a function that is
167+
/// called when the storage is assigned to (like a setter) or
168+
/// read-modified (like materializeForSet), but which works by
169+
/// returning a pointer to a mutable value of the storage type.
170+
/// This kind of accessor also has an addressor kind.
171+
///
172+
/// Addressors are a way of proving more efficient access to storage
173+
/// when it is already stored in memory (but not as a stored member
174+
/// of the type).
175+
ACCESSOR(MutableAddress)
176+
177+
MUTABLE_ADDRESSOR(Unsafe, unsafeMutableAddress)
178+
MUTABLE_ADDRESSOR(Owning, mutableAddressWithOwner)
179+
MUTABLE_ADDRESSOR(NativeOwning, mutableAddressWithNativeOwner)
180+
MUTABLE_ADDRESSOR(NativePinning, mutableAddressWithPinnedNativeOwner)
181+
182+
#ifdef LAST_ACCESSOR
183+
LAST_ACCESSOR(MutableAddress)
184+
#undef LAST_ACCESSOR
185+
#endif
186+
187+
#undef IMMUTABLE_ADDRESSOR
188+
#undef MUTABLE_ADDRESSOR
189+
#undef ANY_ADDRESSOR
190+
#undef OBJC_ACCESSOR
191+
#undef OPAQUE_ACCESSOR
192+
#undef OBSERVING_ACCESSOR
193+
#undef SINGLETON_ACCESSOR
194+
#undef ACCESSOR
195+
#undef ACCESSOR_KEYWORD
196+
#undef SUPPRESS_ARTIFICIAL_ACCESSORS

include/swift/AST/Decl.h

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3949,24 +3949,16 @@ class ProtocolDecl final : public NominalTypeDecl {
39493949
// Note that the values of these enums line up with %select values in
39503950
// diagnostics.
39513951
enum class AccessorKind {
3952-
/// \brief This is a getter for a property or subscript.
3953-
IsGetter = 0,
3954-
/// \brief This is a setter for a property or subscript.
3955-
IsSetter = 1,
3956-
/// \brief This is a willSet specifier for a property.
3957-
IsWillSet = 2,
3958-
/// \brief This is a didSet specifier for a property.
3959-
IsDidSet = 3,
3960-
/// \brief This is a materializeForSet accessor for a property.
3961-
IsMaterializeForSet = 4,
3962-
/// \brief This is an address-family accessor for a property or
3963-
/// subscript. It also has an addressor kind.
3964-
IsAddressor = 5,
3965-
/// \brief This is a mutableAddress-family accessor for a property
3966-
/// or subscript. It also has an addressor kind.
3967-
IsMutableAddressor = 6,
3952+
#define ACCESSOR(ID) ID,
3953+
#define LAST_ACCESSOR(ID) Last = ID
3954+
#include "swift/AST/AccessorKinds.def"
39683955
};
39693956

3957+
static inline IntRange<AccessorKind> allAccessorKinds() {
3958+
return IntRange<AccessorKind>(AccessorKind(0),
3959+
AccessorKind(unsigned(AccessorKind::Last) + 1));
3960+
}
3961+
39703962
/// The safety semantics of this addressor.
39713963
enum class AddressorKind : uint8_t {
39723964
/// \brief This is not an addressor.
@@ -5735,15 +5727,15 @@ class AccessorDecl final : public FuncDecl {
57355727
return AddressorKind(Bits.AccessorDecl.AddressorKind);
57365728
}
57375729

5738-
bool isGetter() const { return getAccessorKind() == AccessorKind::IsGetter; }
5739-
bool isSetter() const { return getAccessorKind() == AccessorKind::IsSetter; }
5730+
bool isGetter() const { return getAccessorKind() == AccessorKind::Get; }
5731+
bool isSetter() const { return getAccessorKind() == AccessorKind::Set; }
57405732
bool isMaterializeForSet() const {
5741-
return getAccessorKind() == AccessorKind::IsMaterializeForSet;
5733+
return getAccessorKind() == AccessorKind::MaterializeForSet;
57425734
}
57435735
bool isAnyAddressor() const {
57445736
auto kind = getAccessorKind();
5745-
return kind == AccessorKind::IsAddressor
5746-
|| kind == AccessorKind::IsMutableAddressor;
5737+
return kind == AccessorKind::Address
5738+
|| kind == AccessorKind::MutableAddress;
57475739
}
57485740

57495741
/// isGetterOrSetter - Determine whether this is specifically a getter or
@@ -5753,8 +5745,8 @@ class AccessorDecl final : public FuncDecl {
57535745
bool isGetterOrSetter() const { return isGetter() || isSetter(); }
57545746

57555747
bool isObservingAccessor() const {
5756-
return getAccessorKind() == AccessorKind::IsDidSet ||
5757-
getAccessorKind() == AccessorKind::IsWillSet;
5748+
return getAccessorKind() == AccessorKind::DidSet ||
5749+
getAccessorKind() == AccessorKind::WillSet;
57585750
}
57595751

57605752
static bool classof(const Decl *D) {

include/swift/AST/DiagnosticsParse.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ ERROR(conflicting_property_addressor,none,
265265
"%select{variable|subscript}0 already has a "
266266
"%select{addressor|mutable addressor}1", (unsigned, unsigned))
267267
ERROR(expected_accessor_name,none,
268-
"expected %select{%error|setter|willSet|didSet}0 parameter name",
268+
"expected %select{%error|setter|%error|willSet|didSet}0 parameter name",
269269
(unsigned))
270270
ERROR(expected_rparen_set_name,none,
271271
"expected ')' after setter parameter name",())

include/swift/Parse/Parser.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -878,12 +878,9 @@ class Parser {
878878

879879
struct ParsedAccessors {
880880
SourceLoc LBLoc, RBLoc;
881-
AccessorDecl *Get = nullptr;
882-
AccessorDecl *Set = nullptr;
883-
AccessorDecl *Addressor = nullptr;
884-
AccessorDecl *MutableAddressor = nullptr;
885-
AccessorDecl *WillSet = nullptr;
886-
AccessorDecl *DidSet = nullptr;
881+
882+
#define ACCESSOR(ID) AccessorDecl *ID = nullptr;
883+
#include "swift/AST/AccessorKinds.def"
887884

888885
void record(Parser &P, AbstractStorageDecl *storage, bool invalid,
889886
ParseDeclOptions flags, SourceLoc staticLoc,

include/swift/Serialization/ModuleFile.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -568,16 +568,14 @@ class ModuleFile
568568
/// Populates TopLevelIDs for name lookup.
569569
void buildTopLevelDeclMap();
570570

571+
struct AccessorRecord {
572+
SmallVector<serialization::DeclID, 8> IDs;
573+
};
574+
571575
/// Sets the accessors for \p storage based on \p rawStorageKind.
572576
void configureStorage(AbstractStorageDecl *storage,
573577
unsigned rawStorageKind,
574-
serialization::DeclID getter,
575-
serialization::DeclID setter,
576-
serialization::DeclID materializeForSet,
577-
serialization::DeclID addressor,
578-
serialization::DeclID mutableAddressor,
579-
serialization::DeclID willSet,
580-
serialization::DeclID didSet);
578+
AccessorRecord &accessors);
581579

582580
public:
583581
/// Loads a module from the given memory buffer.

include/swift/Serialization/ModuleFormat.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const uint16_t VERSION_MAJOR = 0;
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
5757
/// Don't worry about adhering to the 80-column limit for this line.
58-
const uint16_t VERSION_MINOR = 419; // Last change: Remove discriminator from LocalDeclTableInfo.
58+
const uint16_t VERSION_MINOR = 420; // Last change: accessor refactor
5959

6060
using DeclIDField = BCFixed<31>;
6161

@@ -188,16 +188,18 @@ using OperatorKindField = BCFixed<3>;
188188
// These IDs must \em not be renumbered or reordered without incrementing
189189
// VERSION_MAJOR.
190190
enum AccessorKind : uint8_t {
191-
Getter = 0,
192-
Setter,
191+
Get = 0,
192+
Set,
193193
WillSet,
194194
DidSet,
195195
MaterializeForSet,
196-
Addressor,
197-
MutableAddressor,
196+
Address,
197+
MutableAddress,
198198
};
199199
using AccessorKindField = BCFixed<3>;
200200

201+
using AccessorCountField = BCFixed<3>;
202+
201203
// These IDs must \em not be renumbered or reordered without incrementing
202204
// VERSION_MAJOR.
203205
enum CtorInitializerKind : uint8_t {
@@ -956,18 +958,12 @@ namespace decls_block {
956958
BCFixed<1>, // is getter mutating?
957959
BCFixed<1>, // is setter mutating?
958960
StorageKindField, // StorageKind
961+
AccessorCountField, // number of accessors
959962
TypeIDField, // interface type
960-
DeclIDField, // getter
961-
DeclIDField, // setter
962-
DeclIDField, // materializeForSet
963-
DeclIDField, // addressor
964-
DeclIDField, // mutableAddressor
965-
DeclIDField, // willset
966-
DeclIDField, // didset
967963
DeclIDField, // overridden decl
968964
AccessLevelField, // access level
969965
AccessLevelField, // setter access, if applicable
970-
BCArray<TypeIDField> // dependencies
966+
BCArray<TypeIDField> // accessors and dependencies
971967
>;
972968

973969
using ParamLayout = BCRecordLayout<
@@ -1099,20 +1095,15 @@ namespace decls_block {
10991095
BCFixed<1>, // is getter mutating?
11001096
BCFixed<1>, // is setter mutating?
11011097
StorageKindField, // StorageKind
1098+
AccessorCountField, // number of accessors
11021099
GenericEnvironmentIDField, // generic environment
11031100
TypeIDField, // interface type
1104-
DeclIDField, // getter
1105-
DeclIDField, // setter
1106-
DeclIDField, // materializeForSet
1107-
DeclIDField, // addressor
1108-
DeclIDField, // mutableAddressor
1109-
DeclIDField, // willSet
1110-
DeclIDField, // didSet
11111101
DeclIDField, // overridden decl
11121102
AccessLevelField, // access level
11131103
AccessLevelField, // setter access, if applicable
11141104
BCVBR<5>, // number of parameter name components
11151105
BCArray<IdentifierIDField> // name components,
1106+
// followed by DeclID accessors,
11161107
// followed by TypeID dependencies
11171108
// Trailed by:
11181109
// - generic parameters, if any

0 commit comments

Comments
 (0)