Skip to content

Commit f933552

Browse files
committed
[NFC] AST: Added AccessorKind predicates.
And replaced direct comparisons with calls to predicates. In preparation for introducing parallel non-underscored accessors.
1 parent 6b57a9f commit f933552

File tree

7 files changed

+73
-20
lines changed

7 files changed

+73
-20
lines changed

include/swift/AST/AnyFunctionRef.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,10 @@ class AnyFunctionRef {
260260
->getReferenceStorageReferent();
261261
if (mapIntoContext)
262262
valueTy = AD->mapTypeIntoContext(valueTy);
263-
YieldTypeFlags flags(AD->getAccessorKind() == AccessorKind::Modify
264-
? ParamSpecifier::InOut
265-
: ParamSpecifier::LegacyShared);
263+
YieldTypeFlags flags(
264+
isYieldingDefaultMutatingAccessor(AD->getAccessorKind())
265+
? ParamSpecifier::InOut
266+
: ParamSpecifier::LegacyShared);
266267
buffer.push_back(AnyFunctionType::Yield(valueTy, flags));
267268
return buffer;
268269
}
@@ -304,4 +305,3 @@ struct DenseMapInfo<swift::AnyFunctionRef> {
304305
}
305306

306307
#endif // LLVM_SWIFT_AST_ANY_FUNCTION_REF_H
307-

include/swift/AST/StorageImpl.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,57 @@ enum class AccessorKind {
4949
#undef LAST_ACCESSOR
5050
};
5151

52+
inline bool isYieldingAccessor(AccessorKind kind) {
53+
switch (kind) {
54+
case AccessorKind::Read:
55+
case AccessorKind::Modify:
56+
return true;
57+
case AccessorKind::Get:
58+
case AccessorKind::DistributedGet:
59+
case AccessorKind::Set:
60+
case AccessorKind::WillSet:
61+
case AccessorKind::DidSet:
62+
case AccessorKind::Address:
63+
case AccessorKind::MutableAddress:
64+
case AccessorKind::Init:
65+
return false;
66+
}
67+
}
68+
69+
inline bool isYieldingDefaultNonmutatingAccessor(AccessorKind kind) {
70+
switch (kind) {
71+
case AccessorKind::Read:
72+
return true;
73+
case AccessorKind::Get:
74+
case AccessorKind::DistributedGet:
75+
case AccessorKind::Set:
76+
case AccessorKind::Modify:
77+
case AccessorKind::WillSet:
78+
case AccessorKind::DidSet:
79+
case AccessorKind::Address:
80+
case AccessorKind::MutableAddress:
81+
case AccessorKind::Init:
82+
return false;
83+
}
84+
}
85+
86+
inline bool isYieldingDefaultMutatingAccessor(AccessorKind kind) {
87+
switch (kind) {
88+
case AccessorKind::Modify:
89+
return true;
90+
case AccessorKind::Get:
91+
case AccessorKind::DistributedGet:
92+
case AccessorKind::Set:
93+
case AccessorKind::Read:
94+
case AccessorKind::WillSet:
95+
case AccessorKind::DidSet:
96+
case AccessorKind::Address:
97+
case AccessorKind::MutableAddress:
98+
case AccessorKind::Init:
99+
return false;
100+
}
101+
}
102+
52103
const unsigned NumAccessorKinds = unsigned(AccessorKind::Last) + 1;
53104

54105
static inline IntRange<AccessorKind> allAccessorKinds() {

lib/AST/LifetimeDependence.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static ValueOwnership getLoweredOwnership(AbstractFunctionDecl *afd) {
9898
}
9999
if (auto *ad = dyn_cast<AccessorDecl>(afd)) {
100100
if (ad->getAccessorKind() == AccessorKind::Set ||
101-
ad->getAccessorKind() == AccessorKind::Modify) {
101+
isYieldingDefaultMutatingAccessor(ad->getAccessorKind())) {
102102
return ValueOwnership::InOut;
103103
}
104104
}

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,15 +2188,15 @@ static void destructureYieldsForCoroutine(TypeConverter &TC,
21882188
return;
21892189

21902190
// 'modify' yields an inout of the target type.
2191-
if (accessor->getAccessorKind() == AccessorKind::Modify) {
2191+
if (isYieldingDefaultMutatingAccessor(accessor->getAccessorKind())) {
21922192
auto loweredValueTy =
21932193
TC.getLoweredType(origType, canValueType, expansion);
21942194
yields.push_back(SILYieldInfo(loweredValueTy.getASTType(),
21952195
ParameterConvention::Indirect_Inout));
21962196
} else {
21972197
// 'read' yields a borrowed value of the target type, destructuring
21982198
// tuples as necessary.
2199-
assert(accessor->getAccessorKind() == AccessorKind::Read);
2199+
assert(isYieldingDefaultNonmutatingAccessor(accessor->getAccessorKind()));
22002200
destructureYieldsForReadAccessor(TC, expansion, origType, canValueType,
22012201
yields);
22022202
}

lib/SILGen/SILGenLValue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2227,7 +2227,7 @@ namespace {
22272227
auto decl = cast<AccessorDecl>(Accessor.getFuncDecl());
22282228

22292229
// 'modify' always returns an address of the right type.
2230-
if (decl->getAccessorKind() == AccessorKind::Modify) {
2230+
if (isYieldingDefaultMutatingAccessor(decl->getAccessorKind())) {
22312231
assert(yields.size() == 1);
22322232
return yields[0];
22332233
}

lib/SILOptimizer/Analysis/DifferentiableActivityAnalysis.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ void DifferentiableActivityInfo::propagateVariedInwardsThroughProjections(
273273
// the `inout` argument is a safe over-approximation but not always true.
274274
if (auto *bai = dyn_cast<BeginApplyInst>(inst)) {
275275
if (auto *calleeFn = bai->getCalleeFunction()) {
276-
if (getAccessorKind(calleeFn) == AccessorKind::Modify) {
276+
auto kind = getAccessorKind(calleeFn);
277+
if (kind && isYieldingDefaultMutatingAccessor(*kind))
277278
for (auto inoutArg : bai->getInoutArguments())
278279
propagateVariedInwardsThroughProjections(inoutArg, i);
279-
}
280280
}
281281
}
282282
return;
@@ -351,10 +351,12 @@ void DifferentiableActivityInfo::propagateUseful(
351351
// Note: the assumption that yielded addresses are always a projection into
352352
// the `inout` argument is a safe over-approximation but not always true.
353353
if (auto *bai = dyn_cast<BeginApplyInst>(inst)) {
354-
if (auto *calleeFn = bai->getCalleeFunction())
355-
if (getAccessorKind(calleeFn) == AccessorKind::Modify)
354+
if (auto *calleeFn = bai->getCalleeFunction()) {
355+
auto kind = getAccessorKind(calleeFn);
356+
if (kind && isYieldingDefaultMutatingAccessor(*kind))
356357
for (auto yield : bai->getYieldedValues())
357358
setUsefulAndPropagateToOperands(yield, i);
359+
}
358360
}
359361
// Propagate usefulness through apply site arguments.
360362
for (auto arg : applySite.getArgumentsWithoutIndirectResults())

lib/Sema/TypeCheckStorage.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ static Expr *buildStorageReference(AccessorDecl *accessor,
11281128

11291129
// Check for availability of wrappedValue.
11301130
if (accessor->getAccessorKind() == AccessorKind::Get ||
1131-
accessor->getAccessorKind() == AccessorKind::Read) {
1131+
isYieldingDefaultNonmutatingAccessor(accessor->getAccessorKind())) {
11321132
if (wrappedValue->getAttrs().getUnavailable(ctx)) {
11331133
ExportContext where = ExportContext::forDeclSignature(var);
11341134
diagnoseExplicitUnavailability(
@@ -2114,7 +2114,7 @@ synthesizeCoroutineAccessorBody(AccessorDecl *accessor, ASTContext &ctx) {
21142114

21152115
// If this is a variable with an attached property wrapper, then
21162116
// the accessors need to yield the wrappedValue or projectedValue.
2117-
if (accessor->getAccessorKind() == AccessorKind::Read ||
2117+
if (isYieldingDefaultNonmutatingAccessor(accessor->getAccessorKind()) ||
21182118
storageReadWriteImpl == ReadWriteImplKind::Modify) {
21192119
if (auto var = dyn_cast<VarDecl>(storage)) {
21202120
if (var->hasAttachedPropertyWrapper()) {
@@ -2131,7 +2131,8 @@ synthesizeCoroutineAccessorBody(AccessorDecl *accessor, ASTContext &ctx) {
21312131
SourceLoc loc = storage->getLoc();
21322132
SmallVector<ASTNode, 1> body;
21332133

2134-
bool isModify = accessor->getAccessorKind() == AccessorKind::Modify;
2134+
bool isModify =
2135+
isYieldingDefaultMutatingAccessor(accessor->getAccessorKind());
21352136

21362137
// Special-case for a modify coroutine of a simple stored property with
21372138
// observers. We can yield a borrowed copy of the underlying storage
@@ -2411,12 +2412,12 @@ static AccessorDecl *
24112412
createCoroutineAccessorPrototype(AbstractStorageDecl *storage,
24122413
AccessorKind kind,
24132414
ASTContext &ctx) {
2414-
assert(kind == AccessorKind::Read || kind == AccessorKind::Modify);
2415+
assert(isYieldingAccessor(kind));
24152416

24162417
SourceLoc loc = storage->getLoc();
24172418

24182419
bool isMutating = storage->isGetterMutating();
2419-
if (kind == AccessorKind::Modify)
2420+
if (isYieldingDefaultMutatingAccessor(kind))
24202421
isMutating |= storage->isSetterMutating();
24212422

24222423
auto dc = storage->getDeclContext();
@@ -2453,7 +2454,7 @@ createCoroutineAccessorPrototype(AbstractStorageDecl *storage,
24532454
if (FuncDecl *getter = storage->getParsedAccessor(AccessorKind::Get)) {
24542455
asAvailableAs.push_back(getter);
24552456
}
2456-
if (kind == AccessorKind::Modify) {
2457+
if (isYieldingDefaultMutatingAccessor(kind)) {
24572458
if (FuncDecl *setter = storage->getParsedAccessor(AccessorKind::Set)) {
24582459
asAvailableAs.push_back(setter);
24592460
}
@@ -2467,7 +2468,7 @@ createCoroutineAccessorPrototype(AbstractStorageDecl *storage,
24672468
asAvailableAs, ctx);
24682469

24692470
// A modify coroutine should have the same SPI visibility as the setter.
2470-
if (kind == AccessorKind::Modify) {
2471+
if (isYieldingDefaultMutatingAccessor(kind)) {
24712472
if (FuncDecl *setter = storage->getParsedAccessor(AccessorKind::Set))
24722473
applyInferredSPIAccessControlAttr(accessor, setter, ctx);
24732474
}
@@ -3933,4 +3934,3 @@ bool SimpleDidSetRequest::evaluate(Evaluator &evaluator,
39333934
}
39343935
return false;
39353936
}
3936-

0 commit comments

Comments
 (0)