Skip to content

Rename accessor kinds from IsGetter -> IsGet, etc. #17212

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 1 commit into from
Jun 14, 2018
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
196 changes: 196 additions & 0 deletions include/swift/AST/AccessorKinds.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
//===--- AccessorKinds.def - Swift accessor metaprogramming -----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines macros used for macro-metaprogramming with accessor kinds.
//
//===----------------------------------------------------------------------===//

#if defined(ACCESSOR) && defined(ACCESSOR_KEYWORD)
#error do not define both ACCESSOR and ACCESSOR_KEYWORD
#elif !defined(ACCESSOR) && !defined(ACCESSOR_KEYWORD)
#error must define either ACCESSOR or ACCESSOR_KEYWORD
#endif

/// ACCESSOR(ID)
/// There is an accessor with the enumerator value AccessorKind::ID.
#if !defined(ACCESSOR) && defined(ACCESSOR_KEYWORD)
#define ACCESSOR(ID)
#endif

/// SINGLETON_ACCESSOR(ID, KEYWORD)
/// The given accessor has only one matching accessor keyword.
///
/// Defaults to ACCESSOR(ID) or ACCESSOR_KEYWORD(KEYWORD), depending on which
/// is defined.
#ifndef SINGLETON_ACCESSOR
#if defined(ACCESSOR_KEYWORD)
#define SINGLETON_ACCESSOR(ID, KEYWORD) ACCESSOR_KEYWORD(KEYWORD)
#else
#define SINGLETON_ACCESSOR(ID, KEYWORD) ACCESSOR(ID)
#endif
#endif

/// OBSERVING_ACCESSOR(ID, KEYWORD)
/// The given accessor is an observing accessor.
///
/// Defaults to SINGLETON_ACCESSOR(ID, KEYWORD).
#ifndef OBSERVING_ACCESSOR
#define OBSERVING_ACCESSOR(ID, KEYWORD) SINGLETON_ACCESSOR(ID, KEYWORD)
#endif

/// OPAQUE_ACCESSOR(ID, KEYWORD)
/// The given accessor is used in the opaque access pattern and is created
/// for (mutable) storage whenever its implementation is unknown, such as
/// when it is resilient, overridable, or accessed through a protocol.
///
/// Defaults to SINGLETON_ACCESSOR(ID, KEYWORD).
#ifndef OPAQUE_ACCESSOR
#define OPAQUE_ACCESSOR(ID, KEYWORD) SINGLETON_ACCESSOR(ID, KEYWORD)
#endif

/// OBJC_ACCESSOR(ID, KEYWORD)
/// The given accessor is used in Objective-C, i.e. it is a getter or setter.
///
/// Defaults to OPAQUE_ACCESSOR(ID, KEYWORD).
#ifndef OBJC_ACCESSOR
#define OBJC_ACCESSOR(ID, KEYWORD) OPAQUE_ACCESSOR(ID, KEYWORD)
#endif

/// ANY_ADDRESSOR(ACCESSOR_ID, ADDRESSOR_ID, KEYWORD)
/// The given keyword corresponds to an addressor of the given kind.
///
/// Defaults to ACCESSOR_KEYWORD(KEYWORD) if that is defined; otherwise
/// defaults to nothing.
#ifndef ANY_ADDRESSOR
#if defined(ACCESSOR_KEYWORD)
#define ANY_ADDRESSOR(ACCESSOR_ID, ADDRESSOR_ID, KEYWORD) \
ACCESSOR_KEYWORD(KEYWORD)
#else
#define ANY_ADDRESSOR(ACCESSOR_ID, ADDRESSOR_ID, KEYWORD)
#endif
#endif

/// IMMUTABLE_ADDRESSOR(ADDRESSOR_ID, KEYWORD)
/// The given keyword corresponds to an immutable addressor of the given kind.
///
/// DEfaults to ANY_ADDRESSOR(Address, ADDRESSOR_ID, KEYWORD).
#ifndef IMMUTABLE_ADDRESSOR
#define IMMUTABLE_ADDRESSOR(ADDRESSOR_ID, KEYWORD) \
ANY_ADDRESSOR(Address, ADDRESSOR_ID, KEYWORD)
#endif

/// MUTABLE_ADDRESSOR(ADDRESSOR_ID, KEYWORD)
/// The given keyword corresponds to a mutable addressor of the given kind.
///
/// DEfaults to ANY_ADDRESSOR(MutableAddress, ADDRESSOR_ID, KEYWORD).
#ifndef MUTABLE_ADDRESSOR
#define MUTABLE_ADDRESSOR(ADDRESSOR_ID, KEYWORD) \
ANY_ADDRESSOR(MutableAddress, ADDRESSOR_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.
///
/// If the storage is not implemented with a getter, a getter can
/// always be synthesized. However, this will not be true when
/// move-only types are introduced.
OBJC_ACCESSOR(Get, get)

/// This is a setter: a function that is called when a value is assigned
/// to the storage. It takes a value of the storage type as its
/// primary parameter, in addition to any index values that may be
/// required for a subscript.
///
/// If the storage is not implemented with a setter, a setter can
/// always be synthesized if the storage is mutable at all.
OBJC_ACCESSOR(Set, set)

/// This is a materializeForSet accessor: a function which is called
/// when a combined read-modify operation is performed on the storage,
/// such as passing it as an inout argument (which includes calling
/// a mutating function on it). It is essentially a SILGen-lowered
/// coroutine that yields a pointer to a mutable value of the storage
/// type.
///
/// We do not allow users to provide materializeForSet. It can always
/// be synthesized if the storage is mutable at all.
#if !(SUPPRESS_ARTIFICIAL_ACCESSORS)
OPAQUE_ACCESSOR(MaterializeForSet, materializeForSet)
#endif

/// 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.
///
/// willSet is essentially sugar for implementing a certain common
/// setter idiom.
OBSERVING_ACCESSOR(WillSet, willSet)

/// This is a didSet observer: a function which "decorates" an
/// underlying assignment operation by being called after the
/// operation when a value is assigned to the storage.
///
/// didSet is essentially sugar for implementing a certain common
/// setter idiom.
OBSERVING_ACCESSOR(DidSet, didSet)

/// This is an address-family accessor: a function that is called when
/// a value is loaded from the storage, like a getter, but which works
/// by returning a pointer to an immutable value of the storage type.
/// This kind of accessor also has an addressor kind.
///
/// Addressors are a way of proving more efficient access to storage
/// when it is already stored in memory (but not as a stored member
/// of the type).
ACCESSOR(Address)

IMMUTABLE_ADDRESSOR(Unsafe, unsafeAddress)
IMMUTABLE_ADDRESSOR(Owning, addressWithOwner)
IMMUTABLE_ADDRESSOR(NativeOwning, addressWithNativeOwner)
IMMUTABLE_ADDRESSOR(NativePinning, addressWithPinnedNativeOwner)

/// This is a mutableAddress-family accessor: a function that is
/// called when the storage is assigned to (like a setter) or
/// read-modified (like materializeForSet), but which works by
/// returning a pointer to a mutable value of the storage type.
/// This kind of accessor also has an addressor kind.
///
/// Addressors are a way of proving more efficient access to storage
/// when it is already stored in memory (but not as a stored member
/// of the type).
ACCESSOR(MutableAddress)

MUTABLE_ADDRESSOR(Unsafe, unsafeMutableAddress)
MUTABLE_ADDRESSOR(Owning, mutableAddressWithOwner)
MUTABLE_ADDRESSOR(NativeOwning, mutableAddressWithNativeOwner)
MUTABLE_ADDRESSOR(NativePinning, mutableAddressWithPinnedNativeOwner)

#ifdef LAST_ACCESSOR
LAST_ACCESSOR(MutableAddress)
#undef LAST_ACCESSOR
#endif

#undef IMMUTABLE_ADDRESSOR
#undef MUTABLE_ADDRESSOR
#undef ANY_ADDRESSOR
#undef OBJC_ACCESSOR
#undef OPAQUE_ACCESSOR
#undef OBSERVING_ACCESSOR
#undef SINGLETON_ACCESSOR
#undef ACCESSOR
#undef ACCESSOR_KEYWORD
#undef SUPPRESS_ARTIFICIAL_ACCESSORS
38 changes: 15 additions & 23 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3949,24 +3949,16 @@ class ProtocolDecl final : public NominalTypeDecl {
// Note that the values of these enums line up with %select values in
// diagnostics.
enum class AccessorKind {
/// \brief This is a getter for a property or subscript.
IsGetter = 0,
/// \brief This is a setter for a property or subscript.
IsSetter = 1,
/// \brief This is a willSet specifier for a property.
IsWillSet = 2,
/// \brief This is a didSet specifier for a property.
IsDidSet = 3,
/// \brief This is a materializeForSet accessor for a property.
IsMaterializeForSet = 4,
/// \brief This is an address-family accessor for a property or
/// subscript. It also has an addressor kind.
IsAddressor = 5,
/// \brief This is a mutableAddress-family accessor for a property
/// or subscript. It also has an addressor kind.
IsMutableAddressor = 6,
#define ACCESSOR(ID) ID,
#define LAST_ACCESSOR(ID) Last = ID
#include "swift/AST/AccessorKinds.def"
};

static inline IntRange<AccessorKind> allAccessorKinds() {
return IntRange<AccessorKind>(AccessorKind(0),
AccessorKind(unsigned(AccessorKind::Last) + 1));
}

/// The safety semantics of this addressor.
enum class AddressorKind : uint8_t {
/// \brief This is not an addressor.
Expand Down Expand Up @@ -5735,15 +5727,15 @@ class AccessorDecl final : public FuncDecl {
return AddressorKind(Bits.AccessorDecl.AddressorKind);
}

bool isGetter() const { return getAccessorKind() == AccessorKind::IsGetter; }
bool isSetter() const { return getAccessorKind() == AccessorKind::IsSetter; }
bool isGetter() const { return getAccessorKind() == AccessorKind::Get; }
bool isSetter() const { return getAccessorKind() == AccessorKind::Set; }
bool isMaterializeForSet() const {
return getAccessorKind() == AccessorKind::IsMaterializeForSet;
return getAccessorKind() == AccessorKind::MaterializeForSet;
}
bool isAnyAddressor() const {
auto kind = getAccessorKind();
return kind == AccessorKind::IsAddressor
|| kind == AccessorKind::IsMutableAddressor;
return kind == AccessorKind::Address
|| kind == AccessorKind::MutableAddress;
}

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

bool isObservingAccessor() const {
return getAccessorKind() == AccessorKind::IsDidSet ||
getAccessorKind() == AccessorKind::IsWillSet;
return getAccessorKind() == AccessorKind::DidSet ||
getAccessorKind() == AccessorKind::WillSet;
}

static bool classof(const Decl *D) {
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ ERROR(conflicting_property_addressor,none,
"%select{variable|subscript}0 already has a "
"%select{addressor|mutable addressor}1", (unsigned, unsigned))
ERROR(expected_accessor_name,none,
"expected %select{%error|setter|willSet|didSet}0 parameter name",
"expected %select{%error|setter|%error|willSet|didSet}0 parameter name",
(unsigned))
ERROR(expected_rparen_set_name,none,
"expected ')' after setter parameter name",())
Expand Down
9 changes: 3 additions & 6 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -878,12 +878,9 @@ class Parser {

struct ParsedAccessors {
SourceLoc LBLoc, RBLoc;
AccessorDecl *Get = nullptr;
AccessorDecl *Set = nullptr;
AccessorDecl *Addressor = nullptr;
AccessorDecl *MutableAddressor = nullptr;
AccessorDecl *WillSet = nullptr;
AccessorDecl *DidSet = nullptr;

#define ACCESSOR(ID) AccessorDecl *ID = nullptr;
#include "swift/AST/AccessorKinds.def"

void record(Parser &P, AbstractStorageDecl *storage, bool invalid,
ParseDeclOptions flags, SourceLoc staticLoc,
Expand Down
12 changes: 5 additions & 7 deletions include/swift/Serialization/ModuleFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,16 +568,14 @@ class ModuleFile
/// Populates TopLevelIDs for name lookup.
void buildTopLevelDeclMap();

struct AccessorRecord {
SmallVector<serialization::DeclID, 8> IDs;
};

/// Sets the accessors for \p storage based on \p rawStorageKind.
void configureStorage(AbstractStorageDecl *storage,
unsigned rawStorageKind,
serialization::DeclID getter,
serialization::DeclID setter,
serialization::DeclID materializeForSet,
serialization::DeclID addressor,
serialization::DeclID mutableAddressor,
serialization::DeclID willSet,
serialization::DeclID didSet);
AccessorRecord &accessors);

public:
/// Loads a module from the given memory buffer.
Expand Down
31 changes: 11 additions & 20 deletions include/swift/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const uint16_t VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t VERSION_MINOR = 419; // Last change: Remove discriminator from LocalDeclTableInfo.
const uint16_t VERSION_MINOR = 420; // Last change: accessor refactor

using DeclIDField = BCFixed<31>;

Expand Down Expand Up @@ -188,16 +188,18 @@ using OperatorKindField = BCFixed<3>;
// These IDs must \em not be renumbered or reordered without incrementing
// VERSION_MAJOR.
enum AccessorKind : uint8_t {
Getter = 0,
Setter,
Get = 0,
Set,
WillSet,
DidSet,
MaterializeForSet,
Addressor,
MutableAddressor,
Address,
MutableAddress,
};
using AccessorKindField = BCFixed<3>;

using AccessorCountField = BCFixed<3>;

// These IDs must \em not be renumbered or reordered without incrementing
// VERSION_MAJOR.
enum CtorInitializerKind : uint8_t {
Expand Down Expand Up @@ -956,18 +958,12 @@ namespace decls_block {
BCFixed<1>, // is getter mutating?
BCFixed<1>, // is setter mutating?
StorageKindField, // StorageKind
AccessorCountField, // number of accessors
TypeIDField, // interface type
DeclIDField, // getter
DeclIDField, // setter
DeclIDField, // materializeForSet
DeclIDField, // addressor
DeclIDField, // mutableAddressor
DeclIDField, // willset
DeclIDField, // didset
DeclIDField, // overridden decl
AccessLevelField, // access level
AccessLevelField, // setter access, if applicable
BCArray<TypeIDField> // dependencies
BCArray<TypeIDField> // accessors and dependencies
>;

using ParamLayout = BCRecordLayout<
Expand Down Expand Up @@ -1099,20 +1095,15 @@ namespace decls_block {
BCFixed<1>, // is getter mutating?
BCFixed<1>, // is setter mutating?
StorageKindField, // StorageKind
AccessorCountField, // number of accessors
GenericEnvironmentIDField, // generic environment
TypeIDField, // interface type
DeclIDField, // getter
DeclIDField, // setter
DeclIDField, // materializeForSet
DeclIDField, // addressor
DeclIDField, // mutableAddressor
DeclIDField, // willSet
DeclIDField, // didSet
DeclIDField, // overridden decl
AccessLevelField, // access level
AccessLevelField, // setter access, if applicable
BCVBR<5>, // number of parameter name components
BCArray<IdentifierIDField> // name components,
// followed by DeclID accessors,
// followed by TypeID dependencies
// Trailed by:
// - generic parameters, if any
Expand Down
Loading