Skip to content

Commit ccca4fd

Browse files
committed
[AST] Add IterableDeclContext::getParsedMembers().
Provide an accessor for retrieving the parsed members, generalizing `ParseMembersRequest` so it can provide the parsed members for deserialized/synthesized declarations as well. This is the counterpart to the recently-generalized `getSemanticMembers()`; together, these should suffice for most (all?) clients of `getMembers()`.
1 parent 650a8b2 commit ccca4fd

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

include/swift/AST/DeclContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,11 @@ class IterableDeclContext {
783783
/// Retrieve the set of members in this context.
784784
DeclRange getMembers() const;
785785

786+
/// Get the members that were syntactically present in the source code,
787+
/// and will not contain any members that are implicitly synthesized by
788+
/// the implementation.
789+
ArrayRef<Decl *> getParsedMembers() const;
790+
786791
/// Get all the members that are semantically within this context,
787792
/// including any implicitly-synthesized members.
788793
/// The resulting list of members will be stable across translation units.

lib/AST/DeclContext.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,15 @@ DeclRange IterableDeclContext::getMembers() const {
793793
return getCurrentMembersWithoutLoading();
794794
}
795795

796+
ArrayRef<Decl *> IterableDeclContext::getParsedMembers() const {
797+
ASTContext &ctx = getASTContext();
798+
auto mutableThis = const_cast<IterableDeclContext *>(this);
799+
return evaluateOrDefault(
800+
ctx.evaluator, ParseMembersRequest{mutableThis},
801+
FingerprintAndMembers())
802+
.members;
803+
}
804+
796805
ArrayRef<Decl *> IterableDeclContext::getSemanticMembers() const {
797806
ASTContext &ctx = getASTContext();
798807
return evaluateOrDefault(
@@ -899,10 +908,7 @@ void IterableDeclContext::loadAllMembers() const {
899908
// members to this context, this call is important for recording the
900909
// dependency edge.
901910
auto mutableThis = const_cast<IterableDeclContext *>(this);
902-
auto members =
903-
evaluateOrDefault(ctx.evaluator, ParseMembersRequest{mutableThis},
904-
FingerprintAndMembers())
905-
.members;
911+
auto members = getParsedMembers();
906912

907913
// If we haven't already done so, add these members to this context.
908914
if (!AddedParsedMembers) {

lib/Parse/ParseRequests.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,29 @@ void swift::simple_display(llvm::raw_ostream &out,
4949
FingerprintAndMembers
5050
ParseMembersRequest::evaluate(Evaluator &evaluator,
5151
IterableDeclContext *idc) const {
52-
SourceFile &sf = *idc->getAsGenericContext()->getParentSourceFile();
53-
unsigned bufferID = *sf.getBufferID();
52+
SourceFile *sf = idc->getAsGenericContext()->getParentSourceFile();
53+
ASTContext &ctx = idc->getDecl()->getASTContext();
54+
if (!sf) {
55+
// If there is no parent source file, this is a deserialized or synthesized
56+
// declaration context, in which case `getMembers()` has all of the members.
57+
// Filter out the implicitly-generated ones.
58+
SmallVector<Decl *, 4> members;
59+
for (auto decl : idc->getMembers()) {
60+
if (!decl->isImplicit()) {
61+
members.push_back(decl);
62+
}
63+
}
64+
65+
return FingerprintAndMembers{None, ctx.AllocateCopy(members)};
66+
}
67+
68+
unsigned bufferID = *sf->getBufferID();
5469

5570
// Lexer diaganostics have been emitted during skipping, so we disable lexer's
5671
// diagnostic engine here.
57-
Parser parser(bufferID, sf, /*No Lexer Diags*/nullptr, nullptr, nullptr);
72+
Parser parser(bufferID, *sf, /*No Lexer Diags*/nullptr, nullptr, nullptr);
5873
// Disable libSyntax creation in the delayed parsing.
5974
parser.SyntaxContext->disable();
60-
ASTContext &ctx = idc->getDecl()->getASTContext();
6175
auto declsAndHash = parser.parseDeclListDelayed(idc);
6276
FingerprintAndMembers fingerprintAndMembers = {declsAndHash.second,
6377
declsAndHash.first};

0 commit comments

Comments
 (0)