Skip to content

Commit 7e21c16

Browse files
authored
Merge pull request #26955 from DougGregor/simplify-lazy-member-parsing
Simplify lazy member parsing
2 parents c1e41a3 + 879d617 commit 7e21c16

File tree

14 files changed

+120
-199
lines changed

14 files changed

+120
-199
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ namespace swift {
6969
class LazyGenericContextData;
7070
class LazyIterableDeclContextData;
7171
class LazyMemberLoader;
72-
class LazyMemberParser;
7372
class LazyResolver;
7473
class PatternBindingDecl;
7574
class PatternBindingInitializer;
@@ -425,12 +424,6 @@ class ASTContext final {
425424
void setLazyResolver(LazyResolver *resolver);
426425

427426
public:
428-
/// Add a lazy parser for resolving members later.
429-
void addLazyParser(LazyMemberParser *parser);
430-
431-
/// Remove a lazy parser.
432-
void removeLazyParser(LazyMemberParser *parser);
433-
434427
/// getIdentifier - Return the uniqued and AST-Context-owned version of the
435428
/// specified string.
436429
Identifier getIdentifier(StringRef Str) const;
@@ -822,12 +815,6 @@ class ASTContext final {
822815
LazyContextData *getOrCreateLazyContextData(const DeclContext *decl,
823816
LazyMemberLoader *lazyLoader);
824817

825-
/// Use the lazy parsers associated with the context to populate the members
826-
/// of the given decl context.
827-
///
828-
/// \param IDC The context whose member decls should be lazily parsed.
829-
std::vector<Decl *> parseMembers(IterableDeclContext *IDC);
830-
831818
/// Get the lazy function data for the given generic context.
832819
///
833820
/// \param lazyLoader If non-null, the lazy loader to use when creating the

include/swift/AST/DeclContext.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ namespace swift {
5151
class GenericParamList;
5252
class LazyResolver;
5353
class LazyMemberLoader;
54-
class LazyMemberParser;
5554
class GenericSignature;
5655
class GenericTypeParamDecl;
5756
class GenericTypeParamType;

include/swift/AST/LazyResolver.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,6 @@ class LazyContextData {
8585
LazyMemberLoader *loader;
8686
};
8787

88-
/// A class that can lazily parse members for an iterable decl context.
89-
class LazyMemberParser {
90-
public:
91-
virtual ~LazyMemberParser() = default;
92-
93-
/// Retrieves the parsed members for the given decl context \p IDC.
94-
virtual std::vector<Decl *> parseMembers(IterableDeclContext *IDC) = 0;
95-
96-
/// Return whether the iterable decl context needs parsing.
97-
virtual bool hasUnparsedMembers(const IterableDeclContext *IDC) = 0;
98-
99-
/// Parse all delayed decl list members.
100-
virtual void parseAllDelayedDeclLists() = 0;
101-
};
102-
10388
/// Context data for generic contexts.
10489
class LazyGenericContextData : public LazyContextData {
10590
public:

include/swift/Parse/Parser.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -976,15 +976,12 @@ class Parser {
976976
bool parseMemberDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
977977
SourceLoc PosBeforeLB,
978978
Diag<> ErrorDiag,
979-
ParseDeclOptions Options,
980979
IterableDeclContext *IDC);
981980

982981
bool canDelayMemberDeclParsing(bool &HasOperatorDeclarations,
983982
bool &HasNestedClassDeclarations);
984983

985984
bool delayParsingDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
986-
SourceLoc PosBeforeLB,
987-
ParseDeclOptions Options,
988985
IterableDeclContext *IDC);
989986

990987
ParserResult<TypeDecl> parseDeclTypeAlias(ParseDeclOptions Flags,

include/swift/Parse/PersistentParserState.h

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@
2626

2727
namespace swift {
2828
class AbstractFunctionDecl;
29-
class LazyMemberParser;
30-
class ASTContext;
3129

3230
/// Parser state persistent across multiple parses.
33-
class PersistentParserState: public LazyMemberParser {
31+
class PersistentParserState {
3432
public:
3533
struct ParserPos {
3634
SourceLoc Loc;
@@ -84,34 +82,11 @@ class PersistentParserState: public LazyMemberParser {
8482
{}
8583
};
8684

87-
class DelayedDeclListState {
88-
friend class PersistentParserState;
89-
friend class Parser;
90-
unsigned Flags;
91-
DeclContext *ParentContext;
92-
ParserPos BodyPos;
93-
SourceLoc BodyEnd;
94-
SavedScope Scope;
95-
96-
SavedScope takeScope() {
97-
return std::move(Scope);
98-
}
99-
100-
public:
101-
DelayedDeclListState(unsigned Flags,
102-
DeclContext *ParentContext, SourceRange BodyRange,
103-
SourceLoc PreviousLoc, SavedScope &&Scope)
104-
: Flags(Flags), ParentContext(ParentContext), BodyPos{BodyRange.Start, PreviousLoc},
105-
BodyEnd(BodyRange.End), Scope(std::move(Scope))
106-
{}
107-
};
108-
10985
bool InPoundLineEnvironment = false;
11086
// FIXME: When condition evaluation moves to a later phase, remove this bit
11187
// and adjust the client call 'performParseOnly'.
11288
bool PerformConditionEvaluation = true;
11389
private:
114-
ASTContext &Ctx;
11590
ScopeInfo ScopeInfo;
11691
using DelayedFunctionBodiesTy =
11792
llvm::DenseMap<AbstractFunctionDecl *,
@@ -123,15 +98,15 @@ class PersistentParserState: public LazyMemberParser {
12398

12499
std::unique_ptr<DelayedDeclState> CodeCompletionDelayedDeclState;
125100

126-
llvm::DenseMap<IterableDeclContext *, std::unique_ptr<DelayedDeclListState>>
127-
DelayedDeclListStates;
101+
std::vector<IterableDeclContext *> DelayedDeclLists;
128102

129103
/// The local context for all top-level code.
130104
TopLevelContext TopLevelCode;
131105

132106
public:
133107
swift::ScopeInfo &getScopeInfo() { return ScopeInfo; }
134-
PersistentParserState(ASTContext &Ctx);
108+
PersistentParserState();
109+
PersistentParserState(ASTContext &ctx) : PersistentParserState() { }
135110
~PersistentParserState();
136111

137112
void delayFunctionBodyParsing(AbstractFunctionDecl *AFD,
@@ -146,15 +121,11 @@ class PersistentParserState: public LazyMemberParser {
146121
DeclContext *ParentContext,
147122
SourceRange BodyRange, SourceLoc PreviousLoc);
148123

149-
void delayDeclList(IterableDeclContext *D, unsigned Flags,
150-
DeclContext *ParentContext,
151-
SourceRange BodyRange, SourceLoc PreviousLoc);
124+
void delayDeclList(IterableDeclContext *D);
152125

153126
void delayTopLevel(TopLevelCodeDecl *TLCD, SourceRange BodyRange,
154127
SourceLoc PreviousLoc);
155128

156-
std::vector<Decl *> parseMembers(IterableDeclContext *IDC) override;
157-
158129
bool hasDelayedDecl() {
159130
return CodeCompletionDelayedDeclState.get() != nullptr;
160131
}
@@ -171,14 +142,7 @@ class PersistentParserState: public LazyMemberParser {
171142
return std::move(CodeCompletionDelayedDeclState);
172143
}
173144

174-
std::unique_ptr<DelayedDeclListState>
175-
takeDelayedDeclListState(IterableDeclContext *IDC);
176-
177-
bool hasUnparsedMembers(const IterableDeclContext *IDC) override {
178-
return DelayedDeclListStates.find(IDC) != DelayedDeclListStates.end();
179-
}
180-
181-
void parseAllDelayedDeclLists() override;
145+
void parseAllDelayedDeclLists();
182146

183147
TopLevelContext &getTopLevelContext() {
184148
return TopLevelCode;

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,6 @@ struct ASTContext::Implementation {
159159
/// The last resolver.
160160
LazyResolver *Resolver = nullptr;
161161

162-
/// The lazy parsers for various input files. We may have separate
163-
/// lazy parsers for imported module files and source files.
164-
llvm::SmallPtrSet<LazyMemberParser*, 2> lazyParsers;
165-
166162
// FIXME: This is a StringMap rather than a StringSet because StringSet
167163
// doesn't allow passing in a pre-existing allocator.
168164
llvm::StringMap<char, llvm::BumpPtrAllocator&> IdentifierTable;
@@ -644,16 +640,6 @@ void ASTContext::setLazyResolver(LazyResolver *resolver) {
644640
getImpl().Resolver = resolver;
645641
}
646642

647-
void ASTContext::addLazyParser(LazyMemberParser *lazyParser) {
648-
getImpl().lazyParsers.insert(lazyParser);
649-
}
650-
651-
void ASTContext::removeLazyParser(LazyMemberParser *lazyParser) {
652-
auto removed = getImpl().lazyParsers.erase(lazyParser);
653-
(void)removed;
654-
assert(removed && "Removing an non-existing lazy parser.");
655-
}
656-
657643
/// getIdentifier - Return the uniqued and AST-Context-owned version of the
658644
/// specified string.
659645
Identifier ASTContext::getIdentifier(StringRef Str) const {
@@ -2015,16 +2001,6 @@ LazyContextData *ASTContext::getOrCreateLazyContextData(
20152001
return entry;
20162002
}
20172003

2018-
std::vector<Decl *> ASTContext::parseMembers(IterableDeclContext *IDC) {
2019-
assert(IDC->hasUnparsedMembers());
2020-
for (auto *p: getImpl().lazyParsers) {
2021-
if (p->hasUnparsedMembers(IDC))
2022-
return p->parseMembers(IDC);
2023-
}
2024-
2025-
return { };
2026-
}
2027-
20282004
LazyIterableDeclContextData *ASTContext::getOrCreateLazyIterableContextData(
20292005
const IterableDeclContext *idc,
20302006
LazyMemberLoader *lazyLoader) {

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ void CompilerInstance::parseAndCheckTypesUpTo(
782782
std::unique_ptr<DelayedParsingCallbacks> DelayedCB{
783783
computeDelayedParsingCallback()};
784784

785-
PersistentState = llvm::make_unique<PersistentParserState>(getASTContext());
785+
PersistentState = llvm::make_unique<PersistentParserState>();
786786

787787
bool hadLoadError = parsePartialModulesAndLibraryFiles(
788788
implicitImports, DelayedCB.get());
@@ -1053,7 +1053,7 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
10531053
MainBufferID);
10541054
}
10551055

1056-
PersistentState = llvm::make_unique<PersistentParserState>(getASTContext());
1056+
PersistentState = llvm::make_unique<PersistentParserState>();
10571057

10581058
SWIFT_DEFER {
10591059
if (ParseDelayedBodyOnEnd)

lib/IDE/REPLCodeCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ doCodeCompletion(SourceFile &SF, StringRef EnteredCode, unsigned *BufferID,
207207
// Parse, typecheck and temporarily insert the incomplete code into the AST.
208208
const unsigned OriginalDeclCount = SF.Decls.size();
209209

210-
PersistentParserState PersistentState(Ctx);
210+
PersistentParserState PersistentState;
211211
std::unique_ptr<DelayedParsingCallbacks> DelayedCB(
212212
new CodeCompleteDelayedCallbacks(Ctx.SourceMgr.getCodeCompletionLoc()));
213213
bool Done;

lib/Immediate/REPL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ class REPLEnvironment {
964964
IRGenOpts(),
965965
SILOpts(),
966966
Input(*this),
967-
PersistentState(CI.getASTContext())
967+
PersistentState()
968968
{
969969
ASTContext &Ctx = CI.getASTContext();
970970
Ctx.LangOpts.EnableAccessControl = false;

0 commit comments

Comments
 (0)