Skip to content

Commit 7c256f2

Browse files
committed
Generalize SourceFileDeclFinder
Strip mention of the SourceFile. Any FileUnit-esque thing that can provide top-level decls and `lookupClassMembers` works here - especially a ModuleDecl.
1 parent e0d67f4 commit 7c256f2

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

lib/AST/FrontendSourceFileDepGraphFactory.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,13 @@ std::string FrontendSourceFileDepGraphFactory::getFingerprint(SourceFile *SF) {
298298
// MARK: FrontendSourceFileDepGraphFactory - adding collections of defined Decls
299299
//==============================================================================
300300
//==============================================================================
301-
// MARK: SourceFileDeclFinder
301+
// MARK: DeclFinder
302302
//==============================================================================
303303

304304
namespace {
305305
/// Takes all the Decls in a SourceFile, and collects them into buckets by
306306
/// groups of DeclKinds. Also casts them to more specific types
307-
/// TODO: Factor with SourceFileDeclFinder
308-
struct SourceFileDeclFinder {
309-
310-
public:
307+
struct DeclFinder {
311308
/// Existing system excludes private decls in some cases.
312309
/// In the future, we might not want to do this, so use bool to decide.
313310
const bool includePrivateDecls;
@@ -324,11 +321,16 @@ struct SourceFileDeclFinder {
324321
ConstPtrPairVec<NominalTypeDecl, ValueDecl> valuesInExtensions;
325322
ConstPtrVec<ValueDecl> classMembers;
326323

324+
using LookupClassMember = llvm::function_ref<void(VisibleDeclConsumer &)>;
325+
326+
public:
327327
/// Construct me and separates the Decls.
328328
// clang-format off
329-
SourceFileDeclFinder(const SourceFile *const SF, const bool includePrivateDecls)
329+
DeclFinder(ArrayRef<Decl *> topLevelDecls,
330+
const bool includePrivateDecls,
331+
LookupClassMember lookupClassMember)
330332
: includePrivateDecls(includePrivateDecls) {
331-
for (const Decl *const D : SF->getTopLevelDecls()) {
333+
for (const Decl *const D : topLevelDecls) {
332334
select<ExtensionDecl, DeclKind::Extension>(D, extensions, false) ||
333335
select<OperatorDecl, DeclKind::InfixOperator, DeclKind::PrefixOperator,
334336
DeclKind::PostfixOperator>(D, operators, false) ||
@@ -345,7 +347,7 @@ struct SourceFileDeclFinder {
345347
findNominalsFromExtensions();
346348
findNominalsInTopNominals();
347349
findValuesInExtensions();
348-
findClassMembers(SF);
350+
findClassMembers(lookupClassMember);
349351
}
350352

351353
private:
@@ -419,7 +421,7 @@ struct SourceFileDeclFinder {
419421
}
420422

421423
/// Class members are needed for dynamic lookup dependency nodes.
422-
void findClassMembers(const SourceFile *const SF) {
424+
void findClassMembers(LookupClassMember lookup) {
423425
struct Collector : public VisibleDeclConsumer {
424426
ConstPtrVec<ValueDecl> &classMembers;
425427
Collector(ConstPtrVec<ValueDecl> &classMembers)
@@ -429,7 +431,7 @@ struct SourceFileDeclFinder {
429431
classMembers.push_back(VD);
430432
}
431433
} collector{classMembers};
432-
SF->lookupClassMembers({}, collector);
434+
lookup(collector);
433435
}
434436

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

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

473-
SourceFileDeclFinder declFinder(SF, includePrivateDeps);
475+
DeclFinder declFinder(SF->getTopLevelDecls(), includePrivateDeps,
476+
[this](VisibleDeclConsumer &consumer) {
477+
SF->lookupClassMembers({}, consumer);
478+
});
474479

475480
addAllDefinedDeclsOfAGivenType<NodeKind::topLevel>(
476481
declFinder.precedenceGroups);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===----- FrontendSourceFileDepGraphFactory.h ------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef FrontendSourceFileDepGraphFactory_h
14+
#define FrontendSourceFileDepGraphFactory_h
15+
16+
#include "swift/AST/AbstractSourceFileDepGraphFactory.h"
17+
namespace swift {
18+
namespace fine_grained_dependencies {
19+
20+
/// Constructs a SourceFileDepGraph from a *real* \c SourceFile
21+
/// Reads the information provided by the frontend and builds the
22+
/// SourceFileDepGraph
23+
24+
class FrontendSourceFileDepGraphFactory
25+
: public AbstractSourceFileDepGraphFactory {
26+
SourceFile *const SF;
27+
const DependencyTracker &depTracker;
28+
29+
public:
30+
FrontendSourceFileDepGraphFactory(SourceFile *SF, StringRef outputPath,
31+
const DependencyTracker &depTracker,
32+
bool alsoEmitDotFile);
33+
34+
~FrontendSourceFileDepGraphFactory() override = default;
35+
36+
private:
37+
static std::string getFingerprint(SourceFile *SF);
38+
39+
static bool computeIncludePrivateDeps(SourceFile *SF);
40+
static std::string getInterfaceHash(SourceFile *SF);
41+
42+
void addAllDefinedDecls() override;
43+
void addAllUsedDecls() override;
44+
45+
/// Given an array of Decls or pairs of them in \p declsOrPairs
46+
/// create node pairs for context and name
47+
template <NodeKind kind, typename ContentsT>
48+
void addAllDefinedDeclsOfAGivenType(std::vector<ContentsT> &contentsVec);
49+
50+
/// At present, only nominals, protocols, and extensions have (body)
51+
/// fingerprints
52+
static Optional<std::string>
53+
getFingerprintIfAny(std::pair<const NominalTypeDecl *, const ValueDecl *>);
54+
static Optional<std::string> getFingerprintIfAny(const Decl *d);
55+
};
56+
57+
} // namespace fine_grained_dependencies
58+
} // namespace swift
59+
60+
#endif /* FrontendSourceFileDepGraphFactory_h */

0 commit comments

Comments
 (0)