|
| 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 |
0 commit comments