Skip to content

Simplify lazy member parsing #26955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ namespace swift {
class LazyGenericContextData;
class LazyIterableDeclContextData;
class LazyMemberLoader;
class LazyMemberParser;
class LazyResolver;
class PatternBindingDecl;
class PatternBindingInitializer;
Expand Down Expand Up @@ -425,12 +424,6 @@ class ASTContext final {
void setLazyResolver(LazyResolver *resolver);

public:
/// Add a lazy parser for resolving members later.
void addLazyParser(LazyMemberParser *parser);

/// Remove a lazy parser.
void removeLazyParser(LazyMemberParser *parser);

/// getIdentifier - Return the uniqued and AST-Context-owned version of the
/// specified string.
Identifier getIdentifier(StringRef Str) const;
Expand Down Expand Up @@ -822,12 +815,6 @@ class ASTContext final {
LazyContextData *getOrCreateLazyContextData(const DeclContext *decl,
LazyMemberLoader *lazyLoader);

/// Use the lazy parsers associated with the context to populate the members
/// of the given decl context.
///
/// \param IDC The context whose member decls should be lazily parsed.
std::vector<Decl *> parseMembers(IterableDeclContext *IDC);

/// Get the lazy function data for the given generic context.
///
/// \param lazyLoader If non-null, the lazy loader to use when creating the
Expand Down
1 change: 0 additions & 1 deletion include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ namespace swift {
class GenericParamList;
class LazyResolver;
class LazyMemberLoader;
class LazyMemberParser;
class GenericSignature;
class GenericTypeParamDecl;
class GenericTypeParamType;
Expand Down
15 changes: 0 additions & 15 deletions include/swift/AST/LazyResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,6 @@ class LazyContextData {
LazyMemberLoader *loader;
};

/// A class that can lazily parse members for an iterable decl context.
class LazyMemberParser {
public:
virtual ~LazyMemberParser() = default;

/// Retrieves the parsed members for the given decl context \p IDC.
virtual std::vector<Decl *> parseMembers(IterableDeclContext *IDC) = 0;

/// Return whether the iterable decl context needs parsing.
virtual bool hasUnparsedMembers(const IterableDeclContext *IDC) = 0;

/// Parse all delayed decl list members.
virtual void parseAllDelayedDeclLists() = 0;
};

/// Context data for generic contexts.
class LazyGenericContextData : public LazyContextData {
public:
Expand Down
3 changes: 0 additions & 3 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -976,15 +976,12 @@ class Parser {
bool parseMemberDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
SourceLoc PosBeforeLB,
Diag<> ErrorDiag,
ParseDeclOptions Options,
IterableDeclContext *IDC);

bool canDelayMemberDeclParsing(bool &HasOperatorDeclarations,
bool &HasNestedClassDeclarations);

bool delayParsingDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
SourceLoc PosBeforeLB,
ParseDeclOptions Options,
IterableDeclContext *IDC);

ParserResult<TypeDecl> parseDeclTypeAlias(ParseDeclOptions Flags,
Expand Down
48 changes: 6 additions & 42 deletions include/swift/Parse/PersistentParserState.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@

namespace swift {
class AbstractFunctionDecl;
class LazyMemberParser;
class ASTContext;

/// Parser state persistent across multiple parses.
class PersistentParserState: public LazyMemberParser {
class PersistentParserState {
public:
struct ParserPos {
SourceLoc Loc;
Expand Down Expand Up @@ -84,34 +82,11 @@ class PersistentParserState: public LazyMemberParser {
{}
};

class DelayedDeclListState {
friend class PersistentParserState;
friend class Parser;
unsigned Flags;
DeclContext *ParentContext;
ParserPos BodyPos;
SourceLoc BodyEnd;
SavedScope Scope;

SavedScope takeScope() {
return std::move(Scope);
}

public:
DelayedDeclListState(unsigned Flags,
DeclContext *ParentContext, SourceRange BodyRange,
SourceLoc PreviousLoc, SavedScope &&Scope)
: Flags(Flags), ParentContext(ParentContext), BodyPos{BodyRange.Start, PreviousLoc},
BodyEnd(BodyRange.End), Scope(std::move(Scope))
{}
};

bool InPoundLineEnvironment = false;
// FIXME: When condition evaluation moves to a later phase, remove this bit
// and adjust the client call 'performParseOnly'.
bool PerformConditionEvaluation = true;
private:
ASTContext &Ctx;
ScopeInfo ScopeInfo;
using DelayedFunctionBodiesTy =
llvm::DenseMap<AbstractFunctionDecl *,
Expand All @@ -123,15 +98,15 @@ class PersistentParserState: public LazyMemberParser {

std::unique_ptr<DelayedDeclState> CodeCompletionDelayedDeclState;

llvm::DenseMap<IterableDeclContext *, std::unique_ptr<DelayedDeclListState>>
DelayedDeclListStates;
std::vector<IterableDeclContext *> DelayedDeclLists;

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

public:
swift::ScopeInfo &getScopeInfo() { return ScopeInfo; }
PersistentParserState(ASTContext &Ctx);
PersistentParserState();
PersistentParserState(ASTContext &ctx) : PersistentParserState() { }
~PersistentParserState();

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

void delayDeclList(IterableDeclContext *D, unsigned Flags,
DeclContext *ParentContext,
SourceRange BodyRange, SourceLoc PreviousLoc);
void delayDeclList(IterableDeclContext *D);

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

std::vector<Decl *> parseMembers(IterableDeclContext *IDC) override;

bool hasDelayedDecl() {
return CodeCompletionDelayedDeclState.get() != nullptr;
}
Expand All @@ -171,14 +142,7 @@ class PersistentParserState: public LazyMemberParser {
return std::move(CodeCompletionDelayedDeclState);
}

std::unique_ptr<DelayedDeclListState>
takeDelayedDeclListState(IterableDeclContext *IDC);

bool hasUnparsedMembers(const IterableDeclContext *IDC) override {
return DelayedDeclListStates.find(IDC) != DelayedDeclListStates.end();
}

void parseAllDelayedDeclLists() override;
void parseAllDelayedDeclLists();

TopLevelContext &getTopLevelContext() {
return TopLevelCode;
Expand Down
24 changes: 0 additions & 24 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@ struct ASTContext::Implementation {
/// The last resolver.
LazyResolver *Resolver = nullptr;

/// The lazy parsers for various input files. We may have separate
/// lazy parsers for imported module files and source files.
llvm::SmallPtrSet<LazyMemberParser*, 2> lazyParsers;

// FIXME: This is a StringMap rather than a StringSet because StringSet
// doesn't allow passing in a pre-existing allocator.
llvm::StringMap<char, llvm::BumpPtrAllocator&> IdentifierTable;
Expand Down Expand Up @@ -644,16 +640,6 @@ void ASTContext::setLazyResolver(LazyResolver *resolver) {
getImpl().Resolver = resolver;
}

void ASTContext::addLazyParser(LazyMemberParser *lazyParser) {
getImpl().lazyParsers.insert(lazyParser);
}

void ASTContext::removeLazyParser(LazyMemberParser *lazyParser) {
auto removed = getImpl().lazyParsers.erase(lazyParser);
(void)removed;
assert(removed && "Removing an non-existing lazy parser.");
}

/// getIdentifier - Return the uniqued and AST-Context-owned version of the
/// specified string.
Identifier ASTContext::getIdentifier(StringRef Str) const {
Expand Down Expand Up @@ -2015,16 +2001,6 @@ LazyContextData *ASTContext::getOrCreateLazyContextData(
return entry;
}

std::vector<Decl *> ASTContext::parseMembers(IterableDeclContext *IDC) {
assert(IDC->hasUnparsedMembers());
for (auto *p: getImpl().lazyParsers) {
if (p->hasUnparsedMembers(IDC))
return p->parseMembers(IDC);
}

return { };
}

LazyIterableDeclContextData *ASTContext::getOrCreateLazyIterableContextData(
const IterableDeclContext *idc,
LazyMemberLoader *lazyLoader) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ void CompilerInstance::parseAndCheckTypesUpTo(
std::unique_ptr<DelayedParsingCallbacks> DelayedCB{
computeDelayedParsingCallback()};

PersistentState = llvm::make_unique<PersistentParserState>(getASTContext());
PersistentState = llvm::make_unique<PersistentParserState>();

bool hadLoadError = parsePartialModulesAndLibraryFiles(
implicitImports, DelayedCB.get());
Expand Down Expand Up @@ -1053,7 +1053,7 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
MainBufferID);
}

PersistentState = llvm::make_unique<PersistentParserState>(getASTContext());
PersistentState = llvm::make_unique<PersistentParserState>();

SWIFT_DEFER {
if (ParseDelayedBodyOnEnd)
Expand Down
2 changes: 1 addition & 1 deletion lib/IDE/REPLCodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ doCodeCompletion(SourceFile &SF, StringRef EnteredCode, unsigned *BufferID,
// Parse, typecheck and temporarily insert the incomplete code into the AST.
const unsigned OriginalDeclCount = SF.Decls.size();

PersistentParserState PersistentState(Ctx);
PersistentParserState PersistentState;
std::unique_ptr<DelayedParsingCallbacks> DelayedCB(
new CodeCompleteDelayedCallbacks(Ctx.SourceMgr.getCodeCompletionLoc()));
bool Done;
Expand Down
2 changes: 1 addition & 1 deletion lib/Immediate/REPL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ class REPLEnvironment {
IRGenOpts(),
SILOpts(),
Input(*this),
PersistentState(CI.getASTContext())
PersistentState()
{
ASTContext &Ctx = CI.getASTContext();
Ctx.LangOpts.EnableAccessControl = false;
Expand Down
Loading