Skip to content

Commit 78bdc95

Browse files
authored
Merge pull request swiftlang#14874 from huonw/at-owned-cleanup
Various refactorings for __owned.
2 parents 9aa7ec1 + 3feb91b commit 78bdc95

31 files changed

+508
-290
lines changed

include/swift/AST/Attr.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ DECL_ATTR(__raw_doc_comment, RawDocComment, OnAnyDecl |
214214
NotSerialized | RejectByParser, /* Not serialized */48)
215215

216216
// Also handles unowned and unowned(weak).
217-
DECL_ATTR(weak, Ownership, OnVar | OnParam | DeclModifier | NotSerialized,
217+
DECL_ATTR(weak, ReferenceOwnership, OnVar | OnParam | DeclModifier | NotSerialized,
218218
/* Not serialized */49)
219-
DECL_ATTR_ALIAS(unowned, Ownership)
219+
DECL_ATTR_ALIAS(unowned, ReferenceOwnership)
220220

221221
DECL_ATTR(effects, Effects, OnFunc | OnConstructor | OnDestructor |
222222
UserInaccessible, 50)

include/swift/AST/Attr.h

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,18 @@ class TypeAttributes {
105105

106106
bool hasConvention() const { return convention.hasValue(); }
107107
StringRef getConvention() const { return *convention; }
108-
109-
bool hasOwnership() const { return getOwnership() != Ownership::Strong; }
110-
Ownership getOwnership() const {
111-
if (has(TAK_sil_weak)) return Ownership::Weak;
112-
if (has(TAK_sil_unowned)) return Ownership::Unowned;
113-
if (has(TAK_sil_unmanaged)) return Ownership::Unmanaged;
114-
return Ownership::Strong;
108+
109+
bool hasOwnership() const {
110+
return getOwnership() != ReferenceOwnership::Strong;
111+
}
112+
ReferenceOwnership getOwnership() const {
113+
if (has(TAK_sil_weak))
114+
return ReferenceOwnership::Weak;
115+
if (has(TAK_sil_unowned))
116+
return ReferenceOwnership::Unowned;
117+
if (has(TAK_sil_unmanaged))
118+
return ReferenceOwnership::Unmanaged;
119+
return ReferenceOwnership::Strong;
115120
}
116121

117122
void clearOwnership() {
@@ -965,24 +970,27 @@ class EffectsAttr : public DeclAttribute {
965970

966971

967972
/// Represents weak/unowned/unowned(unsafe) decl modifiers.
968-
class OwnershipAttr : public DeclAttribute {
969-
const Ownership ownership;
973+
class ReferenceOwnershipAttr : public DeclAttribute {
974+
const ReferenceOwnership ownership;
975+
970976
public:
971-
OwnershipAttr(SourceRange range, Ownership kind)
972-
: DeclAttribute(DAK_Ownership, range.Start, range, /*Implicit=*/false),
973-
ownership(kind) {}
977+
ReferenceOwnershipAttr(SourceRange range, ReferenceOwnership kind)
978+
: DeclAttribute(DAK_ReferenceOwnership, range.Start, range,
979+
/*Implicit=*/false),
980+
ownership(kind) {}
974981

975-
OwnershipAttr(Ownership kind) : OwnershipAttr(SourceRange(), kind) {}
982+
ReferenceOwnershipAttr(ReferenceOwnership kind)
983+
: ReferenceOwnershipAttr(SourceRange(), kind) {}
976984

977-
Ownership get() const { return ownership; }
985+
ReferenceOwnership get() const { return ownership; }
978986

979987
/// Returns a copy of this attribute without any source information.
980-
OwnershipAttr *clone(ASTContext &context) const {
981-
return new (context) OwnershipAttr(get());
988+
ReferenceOwnershipAttr *clone(ASTContext &context) const {
989+
return new (context) ReferenceOwnershipAttr(get());
982990
}
983991

984992
static bool classof(const DeclAttribute *DA) {
985-
return DA->getKind() == DAK_Ownership;
993+
return DA->getKind() == DAK_ReferenceOwnership;
986994
}
987995
};
988996

include/swift/AST/Decl.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4606,7 +4606,20 @@ class VarDecl : public AbstractStorageDecl {
46064606
bool isLet() const { return getSpecifier() == Specifier::Let; }
46074607
/// Is this an immutable 'shared' property?
46084608
bool isShared() const { return getSpecifier() == Specifier::Shared; }
4609-
4609+
4610+
ValueOwnership getValueOwnership() const {
4611+
switch (getSpecifier()) {
4612+
case Specifier::Let:
4613+
return ValueOwnership::Default;
4614+
case Specifier::Var:
4615+
return ValueOwnership::Default;
4616+
case Specifier::InOut:
4617+
return ValueOwnership::InOut;
4618+
case Specifier::Shared:
4619+
return ValueOwnership::Shared;
4620+
}
4621+
}
4622+
46104623
/// Is this an element in a capture list?
46114624
bool isCaptureList() const { return Bits.VarDecl.IsCaptureList; }
46124625

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,7 +1915,7 @@ ERROR(override_argument_name_mismatch,none,
19151915
ERROR(override_ownership_mismatch,none,
19161916
"cannot override %select{strong|weak|unowned|unowned(unsafe)}0 property "
19171917
"with %select{strong|weak|unowned|unowned(unsafe)}1 property",
1918-
(/*Ownership*/unsigned, /*Ownership*/unsigned))
1918+
(/*ReferenceOwnership*/unsigned, /*ReferenceOwnership*/unsigned))
19191919
ERROR(override_dynamic_self_mismatch,none,
19201920
"cannot override a Self return type with a non-Self return type",
19211921
())
@@ -3152,22 +3152,22 @@ ERROR(implicitly_unwrapped_optional_in_illegal_position,none,
31523152
ERROR(invalid_ownership_type,none,
31533153
"'%select{strong|weak|unowned|unowned}0' may only be applied to "
31543154
"class and class-bound protocol types, not %1",
3155-
(/*Ownership*/unsigned, Type))
3155+
(/*ReferenceOwnership*/unsigned, Type))
31563156
ERROR(invalid_ownership_protocol_type,none,
31573157
"'%select{strong|weak|unowned|unowned}0' must not be applied to "
31583158
"non-class-bound %1; consider adding a protocol conformance that has a class bound",
3159-
(/*Ownership*/unsigned, Type))
3159+
(/*ReferenceOwnership*/unsigned, Type))
31603160
ERROR(invalid_weak_ownership_not_optional,none,
31613161
"'weak' variable should have optional type %0", (Type))
31623162
ERROR(invalid_weak_let,none,
31633163
"'weak' must be a mutable variable, because it may change at runtime", ())
31643164
ERROR(ownership_invalid_in_protocols,none,
31653165
"'%select{strong|weak|unowned|unowned}0' cannot be applied to a property declaration in a protocol",
3166-
(/*Ownership*/unsigned))
3166+
(/*ReferenceOwnership*/unsigned))
31673167
WARNING(ownership_invalid_in_protocols_compat_warning,none,
31683168
"'%select{strong|weak|unowned|unowned}0' should not be applied to a property declaration "
31693169
"in a protocol and will be disallowed in future versions",
3170-
(/*Ownership*/unsigned))
3170+
(/*ReferenceOwnership*/unsigned))
31713171

31723172
// required
31733173
ERROR(required_initializer_nonclass,none,

include/swift/AST/Ownership.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- Ownership.h - Swift ASTs for Reference Ownership -------*- C++ -*-===//
1+
//===--- Ownership.h - Swift ASTs for Ownership ---------------*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,9 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
// This file defines common structures for working with the different
14-
// kinds of reference ownership supported by Swift, such as 'weak' and
15-
// 'unowned'.
13+
// This file defines common structures for working with the different kinds of
14+
// reference ownership supported by Swift, such as 'weak' and 'unowned', as well
15+
// as the different kinds of value ownership, such as 'inout' and '__shared'.
1616
//
1717
//===----------------------------------------------------------------------===//
1818

@@ -26,7 +26,7 @@ namespace swift {
2626
/// Different kinds of reference ownership supported by Swift.
2727
// This enum is used in diagnostics. If you add a case here, the diagnostics
2828
// must be updated as well.
29-
enum class Ownership : uint8_t {
29+
enum class ReferenceOwnership : uint8_t {
3030
/// \brief a strong reference (the default semantics)
3131
Strong,
3232

@@ -39,7 +39,17 @@ enum class Ownership : uint8_t {
3939
/// \brief an 'unowned(unsafe)' reference
4040
Unmanaged,
4141
};
42-
42+
43+
/// Different kinds of value ownership supported by Swift.
44+
enum class ValueOwnership : uint8_t {
45+
/// \brief the default ownership (owned)
46+
Default,
47+
/// \brief an 'inout' mutating pointer-like value
48+
InOut,
49+
/// \brief a '__shared' non-mutating pointer-like value
50+
Shared
51+
};
52+
4353
} // end namespace swift
4454

4555
#endif

include/swift/AST/Types.h

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,17 +1594,16 @@ class ParameterTypeFlags {
15941594
return ParameterTypeFlags(OptionSet<ParameterFlags>(raw));
15951595
}
15961596

1597-
ParameterTypeFlags(bool variadic, bool autoclosure, bool escaping, bool inOut, bool shared)
1598-
: value((variadic ? Variadic : 0) |
1599-
(autoclosure ? AutoClosure : 0) |
1597+
ParameterTypeFlags(bool variadic, bool autoclosure, bool escaping,
1598+
ValueOwnership ownership)
1599+
: value((variadic ? Variadic : 0) | (autoclosure ? AutoClosure : 0) |
16001600
(escaping ? Escaping : 0) |
1601-
(inOut ? InOut : 0) |
1602-
(shared ? Shared : 0)) {}
1601+
(ownership == ValueOwnership::InOut ? InOut : 0) |
1602+
(ownership == ValueOwnership::Shared ? Shared : 0)) {}
16031603

16041604
/// Create one from what's present in the parameter type
1605-
inline static ParameterTypeFlags fromParameterType(Type paramTy,
1606-
bool isVariadic,
1607-
bool isShared);
1605+
inline static ParameterTypeFlags
1606+
fromParameterType(Type paramTy, bool isVariadic, ValueOwnership ownership);
16081607

16091608
bool isNone() const { return !value; }
16101609
bool isVariadic() const { return value.contains(Variadic); }
@@ -1613,6 +1612,15 @@ class ParameterTypeFlags {
16131612
bool isInOut() const { return value.contains(InOut); }
16141613
bool isShared() const { return value.contains(Shared); }
16151614

1615+
ValueOwnership getValueOwnership() const {
1616+
if (isInOut())
1617+
return ValueOwnership::InOut;
1618+
else if (isShared())
1619+
return ValueOwnership::Shared;
1620+
1621+
return ValueOwnership::Default;
1622+
}
1623+
16161624
ParameterTypeFlags withVariadic(bool variadic) const {
16171625
return ParameterTypeFlags(variadic ? value | ParameterTypeFlags::Variadic
16181626
: value - ParameterTypeFlags::Variadic);
@@ -4752,15 +4760,18 @@ class ReferenceStorageType : public TypeBase {
47524760
private:
47534761
Type Referent;
47544762
public:
4755-
static ReferenceStorageType *get(Type referent, Ownership ownership,
4763+
static ReferenceStorageType *get(Type referent, ReferenceOwnership ownership,
47564764
const ASTContext &C);
47574765

47584766
Type getReferentType() const { return Referent; }
4759-
Ownership getOwnership() const {
4767+
ReferenceOwnership getOwnership() const {
47604768
switch (getKind()) {
4761-
case TypeKind::WeakStorage: return Ownership::Weak;
4762-
case TypeKind::UnownedStorage: return Ownership::Unowned;
4763-
case TypeKind::UnmanagedStorage: return Ownership::Unmanaged;
4769+
case TypeKind::WeakStorage:
4770+
return ReferenceOwnership::Weak;
4771+
case TypeKind::UnownedStorage:
4772+
return ReferenceOwnership::Unowned;
4773+
case TypeKind::UnmanagedStorage:
4774+
return ReferenceOwnership::Unmanaged;
47644775
default: llvm_unreachable("Unhandled reference storage type");
47654776
}
47664777
}
@@ -4772,10 +4783,10 @@ class ReferenceStorageType : public TypeBase {
47724783
}
47734784
};
47744785
BEGIN_CAN_TYPE_WRAPPER(ReferenceStorageType, Type)
4775-
static CanReferenceStorageType get(CanType referent, Ownership ownership) {
4776-
return CanReferenceStorageType(ReferenceStorageType::get(referent,
4777-
ownership,
4778-
referent->getASTContext()));
4786+
static CanReferenceStorageType get(CanType referent,
4787+
ReferenceOwnership ownership) {
4788+
return CanReferenceStorageType(ReferenceStorageType::get(
4789+
referent, ownership, referent->getASTContext()));
47794790
}
47804791
PROXY_CAN_TYPE_SIMPLE_GETTER(getReferentType)
47814792
END_CAN_TYPE_WRAPPER(ReferenceStorageType, Type)
@@ -4789,8 +4800,8 @@ class UnownedStorageType : public ReferenceStorageType {
47894800

47904801
public:
47914802
static UnownedStorageType *get(Type referent, const ASTContext &C) {
4792-
return static_cast<UnownedStorageType*>(
4793-
ReferenceStorageType::get(referent, Ownership::Unowned, C));
4803+
return static_cast<UnownedStorageType *>(
4804+
ReferenceStorageType::get(referent, ReferenceOwnership::Unowned, C));
47944805
}
47954806

47964807
/// Is this unowned storage type known to be loadable within the given
@@ -4820,8 +4831,8 @@ class UnmanagedStorageType : public ReferenceStorageType {
48204831

48214832
public:
48224833
static UnmanagedStorageType *get(Type referent, const ASTContext &C) {
4823-
return static_cast<UnmanagedStorageType*>(
4824-
ReferenceStorageType::get(referent, Ownership::Unmanaged, C));
4834+
return static_cast<UnmanagedStorageType *>(
4835+
ReferenceStorageType::get(referent, ReferenceOwnership::Unmanaged, C));
48254836
}
48264837

48274838
// Implement isa/cast/dyncast/etc.
@@ -4845,8 +4856,8 @@ class WeakStorageType : public ReferenceStorageType {
48454856

48464857
public:
48474858
static WeakStorageType *get(Type referent, const ASTContext &C) {
4848-
return static_cast<WeakStorageType*>(
4849-
ReferenceStorageType::get(referent, Ownership::Weak, C));
4859+
return static_cast<WeakStorageType *>(
4860+
ReferenceStorageType::get(referent, ReferenceOwnership::Weak, C));
48504861
}
48514862

48524863
// Implement isa/cast/dyncast/etc.
@@ -5165,7 +5176,8 @@ inline TupleTypeElt TupleTypeElt::getWithType(Type T) const {
51655176

51665177
/// Create one from what's present in the parameter decl and type
51675178
inline ParameterTypeFlags
5168-
ParameterTypeFlags::fromParameterType(Type paramTy, bool isVariadic, bool isShared) {
5179+
ParameterTypeFlags::fromParameterType(Type paramTy, bool isVariadic,
5180+
ValueOwnership ownership) {
51695181
bool autoclosure = paramTy->is<AnyFunctionType>() &&
51705182
paramTy->castTo<AnyFunctionType>()->isAutoClosure();
51715183
bool escaping = paramTy->is<AnyFunctionType>() &&
@@ -5174,8 +5186,12 @@ ParameterTypeFlags::fromParameterType(Type paramTy, bool isVariadic, bool isShar
51745186
// decomposition. Start by enabling the assertion there and fixing up those
51755187
// callers, then remove this, then remove
51765188
// ParameterTypeFlags::fromParameterType entirely.
5177-
bool inOut = paramTy->is<InOutType>();
5178-
return {isVariadic, autoclosure, escaping, inOut, isShared};
5189+
if (paramTy->is<InOutType>()) {
5190+
assert(ownership == ValueOwnership::Default ||
5191+
ownership == ValueOwnership::InOut);
5192+
ownership = ValueOwnership::InOut;
5193+
}
5194+
return {isVariadic, autoclosure, escaping, ownership};
51795195
}
51805196

51815197
inline const Type *BoundGenericType::getTrailingObjectsPointer() const {

include/swift/Serialization/ModuleFormat.h

Lines changed: 18 additions & 10 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 = 400; // Last change: sil_property
58+
const uint16_t VERSION_MINOR = 401; // Last change: ValueOwnership
5959

6060
using DeclIDField = BCFixed<31>;
6161

@@ -311,13 +311,22 @@ using AssociativityField = BCFixed<2>;
311311

312312
// These IDs must \em not be renumbered or reordered without incrementing
313313
// VERSION_MAJOR.
314-
enum Ownership : uint8_t {
314+
enum ReferenceOwnership : uint8_t {
315315
Strong = 0,
316316
Weak,
317317
Unowned,
318318
Unmanaged,
319319
};
320-
using OwnershipField = BCFixed<2>;
320+
using ReferenceOwnershipField = BCFixed<2>;
321+
322+
// These IDs must \em not be renumbered or reordered without incrementing
323+
// VERSION_MAJOR.
324+
enum ValueOwnership : uint8_t {
325+
Default = 0,
326+
InOut,
327+
Shared,
328+
};
329+
using ValueOwnershipField = BCFixed<2>;
321330

322331
// These IDs must \em not be renumbered or reordered without incrementing
323332
// VERSION_MAJOR.
@@ -656,8 +665,7 @@ namespace decls_block {
656665
BCFixed<1>, // vararg?
657666
BCFixed<1>, // autoclosure?
658667
BCFixed<1>, // escaping?
659-
BCFixed<1>, // inout?
660-
BCFixed<1> // shared?
668+
ValueOwnershipField // inout, shared or owned?
661669
>;
662670

663671
using TupleTypeLayout = BCRecordLayout<
@@ -671,8 +679,7 @@ namespace decls_block {
671679
BCFixed<1>, // vararg?
672680
BCFixed<1>, // autoclosure?
673681
BCFixed<1>, // escaping?
674-
BCFixed<1>, // inout?
675-
BCFixed<1> // shared?
682+
ValueOwnershipField // inout, shared or owned?
676683
>;
677684

678685
using FunctionTypeLayout = BCRecordLayout<
@@ -800,8 +807,8 @@ namespace decls_block {
800807

801808
using ReferenceStorageTypeLayout = BCRecordLayout<
802809
REFERENCE_STORAGE_TYPE,
803-
OwnershipField, // ownership
804-
TypeIDField // implementation type
810+
ReferenceOwnershipField, // ownership
811+
TypeIDField // implementation type
805812
>;
806813

807814
using UnboundGenericTypeLayout = BCRecordLayout<
@@ -1410,7 +1417,8 @@ namespace decls_block {
14101417
>;
14111418

14121419
// Stub layouts, unused.
1413-
using OwnershipDeclAttrLayout = BCRecordLayout<Ownership_DECL_ATTR>;
1420+
using ReferenceOwnershipDeclAttrLayout
1421+
= BCRecordLayout<ReferenceOwnership_DECL_ATTR>;
14141422
using RawDocCommentDeclAttrLayout = BCRecordLayout<RawDocComment_DECL_ATTR>;
14151423
using AccessControlDeclAttrLayout = BCRecordLayout<AccessControl_DECL_ATTR>;
14161424
using SetterAccessDeclAttrLayout = BCRecordLayout<SetterAccess_DECL_ATTR>;

0 commit comments

Comments
 (0)