Skip to content

Commit a44c77d

Browse files
authored
Merge pull request #66867 from DougGregor/requestify-member-init-properties
2 parents 3ddab3e + 31e0252 commit a44c77d

File tree

13 files changed

+116
-44
lines changed

13 files changed

+116
-44
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
@@ -4924,6 +4924,16 @@ NominalTypeDecl::getInitAccessorProperties() const {
49244924
{});
49254925
}
49264926

4927+
ArrayRef<VarDecl *>
4928+
NominalTypeDecl::getMemberwiseInitProperties() const {
4929+
auto &ctx = getASTContext();
4930+
auto mutableThis = const_cast<NominalTypeDecl *>(this);
4931+
return evaluateOrDefault(
4932+
ctx.evaluator,
4933+
MemberwiseInitPropertiesRequest{mutableThis},
4934+
{});
4935+
}
4936+
49274937
void NominalTypeDecl::collectPropertiesInitializableByInitAccessors(
49284938
std::multimap<VarDecl *, VarDecl *> &result) const {
49294939
for (auto *property : getInitAccessorProperties()) {
@@ -7181,8 +7191,10 @@ bool VarDecl::isMemberwiseInitialized(bool preferDeclaredProperties) const {
71817191
// If this is a computed property with `init` accessor, it's
71827192
// memberwise initializable when it could be used to initialize
71837193
// other stored properties.
7184-
if (auto *init = getAccessor(AccessorKind::Init))
7185-
return init->getAttrs().hasAttribute<InitializesAttr>();
7194+
if (hasInitAccessor()) {
7195+
if (auto *init = getAccessor(AccessorKind::Init))
7196+
return init->getAttrs().hasAttribute<InitializesAttr>();
7197+
}
71867198

71877199
// If this is a computed property, it's not memberwise initialized unless
71887200
// 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/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;

lib/SILGen/SILGenConstructor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
406406
storedProperties.insert(properties.begin(), properties.end());
407407
}
408408

409-
for (auto *member : decl->getMembers()) {
409+
for (auto *member : decl->getAllMembers()) {
410410
auto *field = dyn_cast<VarDecl>(member);
411411
if (!field)
412412
continue;

0 commit comments

Comments
 (0)