Skip to content

Commit 86a26ba

Browse files
authored
Merge pull request #66903 from DougGregor/requestify-member-init-properties-5.9
2 parents a190425 + 11e4433 commit 86a26ba

16 files changed

+149
-46
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,6 +3995,10 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
39953995
/// this type.
39963996
ArrayRef<VarDecl *> getInitAccessorProperties() const;
39973997

3998+
/// Return a collection of all properties that will be part of the memberwise
3999+
/// initializer.
4000+
ArrayRef<VarDecl *> getMemberwiseInitProperties() const;
4001+
39984002
/// Establish a mapping between properties that could be iniitalized
39994003
/// via other properties by means of init accessors. This mapping is
40004004
/// one-to-many because we allow intersecting `initializes(...)`.

include/swift/AST/DeclContext.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,23 @@ class IterableDeclContext {
850850
HasNestedClassDeclarations = 1;
851851
}
852852

853-
/// Retrieve the set of members in this context.
853+
/// Retrieve the current set of members in this context.
854+
///
855+
/// NOTE: This operation is an alias of \c getCurrentMembers() that is considered
856+
/// deprecated. Most clients should not use this or \c getCurrentMembers(), but
857+
/// instead should use one of the projections of members that provides a semantic
858+
/// view, e.g., \c getParsedMembers(), \c getABIMembers(), \c getAllMembers(), and so
859+
/// on.
854860
DeclRange getMembers() const;
855861

862+
/// Retrieve the current set of members in this context, without triggering the
863+
/// creation of new members via code synthesis, macro expansion, etc.
864+
///
865+
/// This operation should only be used in narrow places where any side-effect
866+
/// producing operations have been done earlier. For the most part, this means that
867+
/// it should only be used in the implementation of
868+
DeclRange getCurrentMembers() const;
869+
856870
/// Get the members that were syntactically present in the source code,
857871
/// and will not contain any members that are implicitly synthesized by
858872
/// the implementation.

include/swift/AST/TypeCheckRequests.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,28 @@ class InitAccessorPropertiesRequest :
16931693
bool isCached() const { return true; }
16941694
};
16951695

1696+
/// Request to obtain a list of properties that will be reflected in the parameters of a
1697+
/// memberwise initializer.
1698+
class MemberwiseInitPropertiesRequest :
1699+
public SimpleRequest<MemberwiseInitPropertiesRequest,
1700+
ArrayRef<VarDecl *>(NominalTypeDecl *),
1701+
RequestFlags::Cached> {
1702+
public:
1703+
using SimpleRequest::SimpleRequest;
1704+
1705+
private:
1706+
friend SimpleRequest;
1707+
1708+
ArrayRef<VarDecl *>
1709+
evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;
1710+
1711+
// Evaluation.
1712+
bool evaluate(Evaluator &evaluator, AbstractStorageDecl *decl) const;
1713+
1714+
public:
1715+
bool isCached() const { return true; }
1716+
};
1717+
16961718
class HasStorageRequest :
16971719
public SimpleRequest<HasStorageRequest,
16981720
bool(AbstractStorageDecl *),

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ SWIFT_REQUEST(TypeChecker, StoredPropertiesRequest,
312312
SWIFT_REQUEST(TypeChecker, InitAccessorPropertiesRequest,
313313
ArrayRef<VarDecl *>(NominalTypeDecl *),
314314
Cached, NoLocationInfo)
315+
SWIFT_REQUEST(TypeChecker, MemberwiseInitPropertiesRequest,
316+
ArrayRef<VarDecl *>(NominalTypeDecl *),
317+
Cached, NoLocationInfo)
315318
SWIFT_REQUEST(TypeChecker, StructuralTypeRequest, Type(TypeAliasDecl *), Cached,
316319
NoLocationInfo)
317320
SWIFT_REQUEST(TypeChecker, SuperclassTypeRequest,

include/swift/AST/TypeMemberVisitor.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ class TypeMemberVisitor : public DeclVisitor<ImplClass, RetTy> {
6161

6262
/// A convenience method to visit all the members.
6363
void visitMembers(NominalTypeDecl *D) {
64-
for (Decl *member : D->getMembers()) {
65-
member->visitAuxiliaryDecls([&](Decl *decl) {
66-
asImpl().visit(decl);
67-
});
64+
for (Decl *member : D->getAllMembers()) {
6865
asImpl().visit(member);
6966
}
7067
}

lib/AST/Decl.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4911,6 +4911,16 @@ NominalTypeDecl::getInitAccessorProperties() const {
49114911
{});
49124912
}
49134913

4914+
ArrayRef<VarDecl *>
4915+
NominalTypeDecl::getMemberwiseInitProperties() const {
4916+
auto &ctx = getASTContext();
4917+
auto mutableThis = const_cast<NominalTypeDecl *>(this);
4918+
return evaluateOrDefault(
4919+
ctx.evaluator,
4920+
MemberwiseInitPropertiesRequest{mutableThis},
4921+
{});
4922+
}
4923+
49144924
void NominalTypeDecl::collectPropertiesInitializableByInitAccessors(
49154925
std::multimap<VarDecl *, VarDecl *> &result) const {
49164926
for (auto *property : getInitAccessorProperties()) {
@@ -7168,8 +7178,10 @@ bool VarDecl::isMemberwiseInitialized(bool preferDeclaredProperties) const {
71687178
// If this is a computed property with `init` accessor, it's
71697179
// memberwise initializable when it could be used to initialize
71707180
// other stored properties.
7171-
if (auto *init = getAccessor(AccessorKind::Init))
7172-
return init->getAttrs().hasAttribute<InitializesAttr>();
7181+
if (hasInitAccessor()) {
7182+
if (auto *init = getAccessor(AccessorKind::Init))
7183+
return init->getAttrs().hasAttribute<InitializesAttr>();
7184+
}
71737185

71747186
// If this is a computed property, it's not memberwise initialized unless
71757187
// the caller has asked for the declared properties and it is either a

lib/AST/DeclContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,10 @@ DeclRange IterableDeclContext::getCurrentMembersWithoutLoading() const {
920920
}
921921

922922
DeclRange IterableDeclContext::getMembers() const {
923+
return getCurrentMembers();
924+
}
925+
926+
DeclRange IterableDeclContext::getCurrentMembers() const {
923927
loadAllMembers();
924928

925929
return getCurrentMembersWithoutLoading();

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5594,7 +5594,7 @@ static bool shouldEmitCategory(IRGenModule &IGM, ExtensionDecl *ext) {
55945594
return true;
55955595
}
55965596

5597-
for (auto member : ext->getMembers()) {
5597+
for (auto member : ext->getAllMembers()) {
55985598
if (auto func = dyn_cast<FuncDecl>(member)) {
55995599
if (requiresObjCMethodDescriptor(func))
56005600
return true;

lib/Index/Index.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -706,14 +706,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
706706
return;
707707

708708
unsigned CurLabel = 0;
709-
for (auto Member : TypeContext->getMembers()) {
710-
auto Prop = dyn_cast<VarDecl>(Member);
711-
if (!Prop)
712-
continue;
713-
714-
if (!Prop->isMemberwiseInitialized(/*preferDeclaredProperties=*/true))
715-
continue;
716-
709+
for (auto Prop : TypeContext->getMemberwiseInitProperties()) {
717710
if (CurLabel == Args.size())
718711
break;
719712

lib/Refactoring/Refactoring.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,7 +3415,7 @@ collectMembersForInit(ResolvedCursorInfoPtr CursorInfo,
34153415

34163416
SourceManager &SM = nominalDecl->getASTContext().SourceMgr;
34173417

3418-
for (auto member : nominalDecl->getMembers()) {
3418+
for (auto member : nominalDecl->getMemberwiseInitProperties()) {
34193419
auto varDecl = dyn_cast<VarDecl>(member);
34203420
if (!varDecl) {
34213421
continue;
@@ -3428,10 +3428,6 @@ collectMembersForInit(ResolvedCursorInfoPtr CursorInfo,
34283428
continue;
34293429
}
34303430

3431-
if (!varDecl->isMemberwiseInitialized(/*preferDeclaredProperties=*/true)) {
3432-
continue;
3433-
}
3434-
34353431
auto patternBinding = varDecl->getParentPatternBinding();
34363432
if (!patternBinding)
34373433
continue;

0 commit comments

Comments
 (0)