Skip to content

Commit 3dc4c78

Browse files
committed
Mark more declarations as synthesized
1 parent e27578b commit 3dc4c78

8 files changed

+41
-30
lines changed

include/swift/AST/Decl.h

+16-16
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class alignas(1 << DeclAlignInBits) Decl {
333333
NumElements : 32
334334
);
335335

336-
SWIFT_INLINE_BITFIELD(ValueDecl, Decl, 1+1+1,
336+
SWIFT_INLINE_BITFIELD(ValueDecl, Decl, 1+1+1+1,
337337
AlreadyInLookupTable : 1,
338338

339339
/// Whether we have already checked whether this declaration is a
@@ -342,7 +342,11 @@ class alignas(1 << DeclAlignInBits) Decl {
342342

343343
/// Whether the decl can be accessed by swift users; for instance,
344344
/// a.storage for lazy var a is a decl that cannot be accessed.
345-
IsUserAccessible : 1
345+
IsUserAccessible : 1,
346+
347+
/// Whether this member was synthesized as part of a derived
348+
/// protocol conformance.
349+
Synthesized : 1
346350
);
347351

348352
SWIFT_INLINE_BITFIELD(AbstractStorageDecl, ValueDecl, 1,
@@ -387,7 +391,7 @@ class alignas(1 << DeclAlignInBits) Decl {
387391
SWIFT_INLINE_BITFIELD(SubscriptDecl, VarDecl, 2,
388392
StaticSpelling : 2
389393
);
390-
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+1+1+1+1+1+1+1,
394+
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+1+1+1+1+1+1,
391395
/// \see AbstractFunctionDecl::BodyKind
392396
BodyKind : 3,
393397

@@ -406,10 +410,6 @@ class alignas(1 << DeclAlignInBits) Decl {
406410
/// Whether the function body throws.
407411
Throws : 1,
408412

409-
/// Whether this member was synthesized as part of a derived
410-
/// protocol conformance.
411-
Synthesized : 1,
412-
413413
/// Whether this member's body consists of a single expression.
414414
HasSingleExpressionBody : 1,
415415

@@ -2020,6 +2020,7 @@ class ValueDecl : public Decl {
20202020
Bits.ValueDecl.AlreadyInLookupTable = false;
20212021
Bits.ValueDecl.CheckedRedeclaration = false;
20222022
Bits.ValueDecl.IsUserAccessible = true;
2023+
Bits.ValueDecl.Synthesized = false;
20232024
}
20242025

20252026
// MemberLookupTable borrows a bit from this type
@@ -2057,6 +2058,14 @@ class ValueDecl : public Decl {
20572058
return Bits.ValueDecl.IsUserAccessible;
20582059
}
20592060

2061+
bool isSynthesized() const {
2062+
return Bits.ValueDecl.Synthesized;
2063+
}
2064+
2065+
void setSynthesized(bool value = true) {
2066+
Bits.ValueDecl.Synthesized = value;
2067+
}
2068+
20602069
bool hasName() const { return bool(Name); }
20612070
bool isOperator() const { return Name.isOperator(); }
20622071

@@ -5577,7 +5586,6 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
55775586
Bits.AbstractFunctionDecl.Overridden = false;
55785587
Bits.AbstractFunctionDecl.Async = Async;
55795588
Bits.AbstractFunctionDecl.Throws = Throws;
5580-
Bits.AbstractFunctionDecl.Synthesized = false;
55815589
Bits.AbstractFunctionDecl.HasSingleExpressionBody = false;
55825590
Bits.AbstractFunctionDecl.HasNestedTypeDeclarations = false;
55835591
}
@@ -5784,14 +5792,6 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
57845792
/// vtable.
57855793
bool needsNewVTableEntry() const;
57865794

5787-
bool isSynthesized() const {
5788-
return Bits.AbstractFunctionDecl.Synthesized;
5789-
}
5790-
5791-
void setSynthesized(bool value = true) {
5792-
Bits.AbstractFunctionDecl.Synthesized = value;
5793-
}
5794-
57955795
public:
57965796
/// Retrieve the source range of the function body.
57975797
SourceRange getBodySourceRange() const;

lib/Sema/CodeSynthesis.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ static ConstructorDecl *createImplicitConstructor(NominalTypeDecl *decl,
319319

320320
// Mark implicit.
321321
ctor->setImplicit();
322+
ctor->setSynthesized();
322323
ctor->setAccess(accessLevel);
323324

324325
if (ICK == ImplicitConstructorKind::Memberwise) {

lib/Sema/DerivedConformanceCodable.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ static EnumDecl *synthesizeCodingKeysEnum(DerivedConformance &derived) {
239239
auto *enumDecl = new (C) EnumDecl(SourceLoc(), C.Id_CodingKeys, SourceLoc(),
240240
inherited, nullptr, target);
241241
enumDecl->setImplicit();
242+
enumDecl->setSynthesized();
242243
enumDecl->setAccess(AccessLevel::Private);
243244

244245
// For classes which inherit from something Encodable or Decodable, we
@@ -349,6 +350,7 @@ static VarDecl *createKeyedContainer(ASTContext &C, DeclContext *DC,
349350
auto *containerDecl = new (C) VarDecl(/*IsStatic=*/false, introducer,
350351
SourceLoc(), C.Id_container, DC);
351352
containerDecl->setImplicit();
353+
containerDecl->setSynthesized();
352354
containerDecl->setInterfaceType(containerType);
353355
return containerDecl;
354356
}

lib/Sema/DerivedConformanceDifferentiable.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ static ValueDecl *deriveDifferentiable_method(
333333
/*Async=*/false,
334334
/*Throws=*/false,
335335
/*GenericParams=*/nullptr, params, returnType, parentDC);
336+
funcDecl->setSynthesized();
336337
if (!nominal->getSelfClassDecl())
337338
funcDecl->setSelfAccessKind(SelfAccessKind::Mutating);
338339
funcDecl->setBodySynthesizer(bodySynthesizer.Fn, bodySynthesizer.Context);
@@ -458,6 +459,7 @@ getOrSynthesizeTangentVectorStruct(DerivedConformance &derived, Identifier id) {
458459
/*GenericParams*/ {}, parentDC);
459460
structDecl->setBraces({synthesizedLoc, synthesizedLoc});
460461
structDecl->setImplicit();
462+
structDecl->setSynthesized();
461463
structDecl->copyFormalAccessFrom(nominal, /*sourceIsParentContext*/ true);
462464

463465
// Add stored properties to the `TangentVector` struct.
@@ -467,6 +469,7 @@ getOrSynthesizeTangentVectorStruct(DerivedConformance &derived, Identifier id) {
467469
auto *tangentProperty = new (C) VarDecl(
468470
member->isStatic(), member->getIntroducer(),
469471
/*NameLoc*/ SourceLoc(), member->getName(), structDecl);
472+
tangentProperty->setSynthesized();
470473
// Note: `tangentProperty` is not marked as implicit here, because that
471474
// incorrectly affects memberwise initializer synthesis.
472475
auto memberContextualType =

lib/Sema/DerivedConformanceEquatableHashable.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
882882
new (C) VarDecl(/*IsStatic*/false, VarDecl::Introducer::Var,
883883
SourceLoc(), C.Id_hashValue, parentDC);
884884
hashValueDecl->setInterfaceType(intType);
885+
hashValueDecl->setSynthesized();
885886

886887
ParameterList *params = ParameterList::createEmpty(C);
887888

@@ -894,6 +895,7 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
894895
intType, parentDC);
895896
getterDecl->setImplicit();
896897
getterDecl->setBodySynthesizer(&deriveBodyHashable_hashValue);
898+
getterDecl->setSynthesized();
897899
getterDecl->setIsTransparent(false);
898900

899901
getterDecl->copyFormalAccessFrom(derived.Nominal,

lib/Sema/DerivedConformances.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ DerivedConformance::declareDerivedProperty(Identifier name,
464464
VarDecl(/*IsStatic*/ isStatic, VarDecl::Introducer::Var,
465465
SourceLoc(), name, parentDC);
466466
propDecl->setImplicit();
467+
propDecl->setSynthesized();
467468
propDecl->copyFormalAccessFrom(Nominal, /*sourceIsParentContext*/ true);
468469
propDecl->setInterfaceType(propertyInterfaceType);
469470

lib/Sema/TypeCheckDecl.cpp

+12-13
Original file line numberDiff line numberDiff line change
@@ -2511,18 +2511,18 @@ NamingPatternRequest::evaluate(Evaluator &evaluator, VarDecl *VD) const {
25112511
namespace {
25122512

25132513
// Utility class for deterministically ordering vtable entries for
2514-
// synthesized methods.
2515-
struct SortedFuncList {
2514+
// synthesized declarations.
2515+
struct SortedDeclList {
25162516
using Key = std::tuple<DeclName, std::string>;
2517-
using Entry = std::pair<Key, AbstractFunctionDecl *>;
2517+
using Entry = std::pair<Key, ValueDecl *>;
25182518
SmallVector<Entry, 2> elts;
25192519
bool sorted = false;
25202520

2521-
void add(AbstractFunctionDecl *afd) {
2522-
assert(!isa<AccessorDecl>(afd));
2521+
void add(ValueDecl *vd) {
2522+
assert(!isa<AccessorDecl>(vd));
25232523

2524-
Key key{afd->getName(), afd->getInterfaceType().getString()};
2525-
elts.emplace_back(key, afd);
2524+
Key key{vd->getName(), vd->getInterfaceType()->getCanonicalType().getString()};
2525+
elts.emplace_back(key, vd);
25262526
}
25272527

25282528
bool empty() { return elts.empty(); }
@@ -2603,13 +2603,13 @@ SemanticMembersRequest::evaluate(Evaluator &evaluator,
26032603
}
26042604
}
26052605

2606-
SortedFuncList synthesizedMembers;
2606+
SortedDeclList synthesizedMembers;
26072607

26082608
for (auto *member : idc->getMembers()) {
2609-
if (auto *afd = dyn_cast<AbstractFunctionDecl>(member)) {
2609+
if (auto *vd = dyn_cast<ValueDecl>(member)) {
26102610
// If this is a witness to Actor.enqueue(partialTask:), put it at the
26112611
// beginning of the vtable.
2612-
if (auto func = dyn_cast<FuncDecl>(afd)) {
2612+
if (auto func = dyn_cast<FuncDecl>(vd)) {
26132613
if (func->isActorEnqueuePartialTaskWitness()) {
26142614
result.insert(result.begin(), func);
26152615
continue;
@@ -2618,8 +2618,8 @@ SemanticMembersRequest::evaluate(Evaluator &evaluator,
26182618

26192619
// Add synthesized members to a side table and sort them by their mangled
26202620
// name, since they could have been added to the class in any order.
2621-
if (afd->isSynthesized()) {
2622-
synthesizedMembers.add(afd);
2621+
if (vd->isSynthesized()) {
2622+
synthesizedMembers.add(vd);
26232623
continue;
26242624
}
26252625
}
@@ -2629,7 +2629,6 @@ SemanticMembersRequest::evaluate(Evaluator &evaluator,
26292629

26302630
if (!synthesizedMembers.empty()) {
26312631
synthesizedMembers.sort();
2632-
26332632
for (const auto &pair : synthesizedMembers)
26342633
result.push_back(pair.second);
26352634
}

lib/Sema/TypeCheckStorage.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1901,6 +1901,7 @@ static AccessorDecl *createGetterPrototype(AbstractStorageDecl *storage,
19011901
getterParams,
19021902
Type(),
19031903
storage->getDeclContext());
1904+
getter->setSynthesized();
19041905

19051906
// If we're stealing the 'self' from a lazy initializer, set it now.
19061907
// Note that we don't re-parent the 'self' declaration to be part of
@@ -1950,6 +1951,7 @@ static AccessorDecl *createSetterPrototype(AbstractStorageDecl *storage,
19501951
genericParams, params,
19511952
Type(),
19521953
storage->getDeclContext());
1954+
setter->setSynthesized();
19531955

19541956
if (isMutating)
19551957
setter->setSelfAccessKind(SelfAccessKind::Mutating);
@@ -2060,7 +2062,8 @@ createCoroutineAccessorPrototype(AbstractStorageDecl *storage,
20602062
/*StaticLoc=*/SourceLoc(), StaticSpellingKind::None,
20612063
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
20622064
genericParams, params, retTy, dc);
2063-
2065+
accessor->setSynthesized();
2066+
20642067
if (isMutating)
20652068
accessor->setSelfAccessKind(SelfAccessKind::Mutating);
20662069
else

0 commit comments

Comments
 (0)