Skip to content

[NFC] Some Uncontroversial Frontend Refactorings #34059

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 3 commits into from
Sep 24, 2020
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
2 changes: 1 addition & 1 deletion include/swift/Serialization/SerializedModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
///
/// If the AST cannot be loaded and \p diagLoc is present, a diagnostic is
/// printed. (Note that \p diagLoc is allowed to be invalid.)
FileUnit *
LoadedFile *
loadAST(ModuleDecl &M, Optional<SourceLoc> diagLoc,
StringRef moduleInterfacePath,
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
Expand Down
29 changes: 17 additions & 12 deletions lib/AST/FrontendSourceFileDepGraphFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// it is written to a file which is read by the driver in order to decide which
// source files require recompilation.

#include "swift/AST/FrontendSourceFileDepGraphFactory.h"
#include "FrontendSourceFileDepGraphFactory.h"

// may not all be needed
#include "swift/AST/ASTContext.h"
Expand Down Expand Up @@ -298,16 +298,13 @@ std::string FrontendSourceFileDepGraphFactory::getFingerprint(SourceFile *SF) {
// MARK: FrontendSourceFileDepGraphFactory - adding collections of defined Decls
//==============================================================================
//==============================================================================
// MARK: SourceFileDeclFinder
// MARK: DeclFinder
//==============================================================================

namespace {
/// Takes all the Decls in a SourceFile, and collects them into buckets by
/// groups of DeclKinds. Also casts them to more specific types
/// TODO: Factor with SourceFileDeclFinder
struct SourceFileDeclFinder {

public:
struct DeclFinder {
/// Existing system excludes private decls in some cases.
/// In the future, we might not want to do this, so use bool to decide.
const bool includePrivateDecls;
Expand All @@ -324,11 +321,16 @@ struct SourceFileDeclFinder {
ConstPtrPairVec<NominalTypeDecl, ValueDecl> valuesInExtensions;
ConstPtrVec<ValueDecl> classMembers;

using LookupClassMember = llvm::function_ref<void(VisibleDeclConsumer &)>;

public:
/// Construct me and separates the Decls.
// clang-format off
SourceFileDeclFinder(const SourceFile *const SF, const bool includePrivateDecls)
DeclFinder(ArrayRef<Decl *> topLevelDecls,
const bool includePrivateDecls,
LookupClassMember lookupClassMember)
: includePrivateDecls(includePrivateDecls) {
for (const Decl *const D : SF->getTopLevelDecls()) {
for (const Decl *const D : topLevelDecls) {
select<ExtensionDecl, DeclKind::Extension>(D, extensions, false) ||
select<OperatorDecl, DeclKind::InfixOperator, DeclKind::PrefixOperator,
DeclKind::PostfixOperator>(D, operators, false) ||
Expand All @@ -345,7 +347,7 @@ struct SourceFileDeclFinder {
findNominalsFromExtensions();
findNominalsInTopNominals();
findValuesInExtensions();
findClassMembers(SF);
findClassMembers(lookupClassMember);
}

private:
Expand Down Expand Up @@ -419,7 +421,7 @@ struct SourceFileDeclFinder {
}

/// Class members are needed for dynamic lookup dependency nodes.
void findClassMembers(const SourceFile *const SF) {
void findClassMembers(LookupClassMember lookup) {
struct Collector : public VisibleDeclConsumer {
ConstPtrVec<ValueDecl> &classMembers;
Collector(ConstPtrVec<ValueDecl> &classMembers)
Expand All @@ -429,7 +431,7 @@ struct SourceFileDeclFinder {
classMembers.push_back(VD);
}
} collector{classMembers};
SF->lookupClassMembers({}, collector);
lookup(collector);
}

/// Check \p D to see if it is one of the DeclKinds in the template
Expand Down Expand Up @@ -470,7 +472,10 @@ void FrontendSourceFileDepGraphFactory::addAllDefinedDecls() {

// Many kinds of Decls become top-level depends.

SourceFileDeclFinder declFinder(SF, includePrivateDeps);
DeclFinder declFinder(SF->getTopLevelDecls(), includePrivateDeps,
[this](VisibleDeclConsumer &consumer) {
SF->lookupClassMembers({}, consumer);
});

addAllDefinedDeclsOfAGivenType<NodeKind::topLevel>(
declFinder.precedenceGroups);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===----- FrontendSourceFileDepGraphFactory.h -------------------*- C++ -*-===//
//===----- FrontendSourceFileDepGraphFactory.h ------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/SerializedModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ getOSAndVersionForDiagnostics(const llvm::Triple &triple) {
return {osName, version};
}

FileUnit *SerializedModuleLoaderBase::loadAST(
LoadedFile *SerializedModuleLoaderBase::loadAST(
ModuleDecl &M, Optional<SourceLoc> diagLoc,
StringRef moduleInterfacePath,
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
Expand Down