Skip to content

Commit 2b6d1a8

Browse files
committed
[NFC] Convert the Templates Computing DependencyKeys to the Builder Pattern
It can be quite difficult to tell at a glance just how any particular decl is going to be converted into a key. The space of available template specializations is also 2-dimensional which adds an additional level of difficulty when the time comes to extend or refactor any of them. Unroll all of the templates into a builder that coalesces the commonalities of the ways DependencyKeys are built to combat this.
1 parent dcc1292 commit 2b6d1a8

File tree

4 files changed

+221
-158
lines changed

4 files changed

+221
-158
lines changed

include/swift/AST/FineGrainedDependencies.h

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_AST_FINE_GRAINED_DEPENDENCIES_H
1414
#define SWIFT_AST_FINE_GRAINED_DEPENDENCIES_H
1515

16+
#include "swift/AST/EvaluatorDependencies.h"
1617
#include "swift/Basic/Debug.h"
1718
#include "swift/Basic/Fingerprint.h"
1819
#include "swift/Basic/LLVM.h"
@@ -57,11 +58,14 @@
5758
//==============================================================================
5859

5960
namespace swift {
61+
class Decl;
6062
class DependencyTracker;
6163
class DiagnosticEngine;
6264
class FrontendOptions;
6365
class ModuleDecl;
6466
class SourceFile;
67+
class NominalTypeDecl;
68+
class ValueDecl;
6569

6670
/// Use a new namespace to help keep the experimental code from clashing.
6771
namespace fine_grained_dependencies {
@@ -417,6 +421,57 @@ class DependencyKey {
417421
// For import/export
418422
friend ::llvm::yaml::MappingTraits<DependencyKey>;
419423

424+
public:
425+
class Builder {
426+
private:
427+
const NodeKind kind;
428+
const DeclAspect aspect;
429+
const NominalTypeDecl *context;
430+
StringRef name;
431+
432+
private:
433+
// A private copy constructor so our clients are forced to use the
434+
// move-only builder interface.
435+
explicit Builder(NodeKind kind, DeclAspect aspect,
436+
const NominalTypeDecl *context, StringRef name)
437+
: kind(kind), aspect(aspect), context(context), name(name) {}
438+
439+
public:
440+
/// Creates a DependencyKey::Builder from the given \p kind and \p aspect
441+
/// with a \c null context and empty name.
442+
explicit Builder(NodeKind kind, DeclAspect aspect)
443+
: kind(kind), aspect(aspect), context(nullptr), name("") {}
444+
445+
public:
446+
/// Consumes this builder and returns a dependency key created from its
447+
/// data.
448+
DependencyKey build() &&;
449+
450+
public:
451+
/// Extracts the data from the given \p ref into a this builder.
452+
Builder fromReference(const evaluator::DependencyCollector::Reference &ref);
453+
454+
public:
455+
/// Extracts the context data from the given declaration, if any.
456+
Builder withContext(const Decl *D) &&;
457+
/// Extracts the context data from the given decl-member pair, if any.
458+
Builder withContext(std::pair<const NominalTypeDecl *, const ValueDecl *>
459+
holderAndMember) &&;
460+
461+
public:
462+
/// Copies the name data for the given swiftdeps file into this builder.
463+
Builder withName(StringRef swiftDeps) &&;
464+
/// Copies the name of the given declaration into this builder, if any.
465+
Builder withName(const Decl *decl) &&;
466+
/// Extracts the name from the given decl-member pair, if any.
467+
Builder withName(std::pair<const NominalTypeDecl *, const ValueDecl *>
468+
holderAndMember) &&;
469+
470+
private:
471+
static StringRef getTopLevelName(const Decl *decl);
472+
};
473+
474+
private:
420475
NodeKind kind;
421476
DeclAspect aspect;
422477
/// The mangled context type name of the holder for \ref potentialMember, \ref
@@ -485,10 +540,6 @@ class DependencyKey {
485540
template <NodeKind kind, typename Entity>
486541
static DependencyKey createForProvidedEntityInterface(Entity);
487542

488-
/// Given some type of provided entity compute the context field of the key.
489-
template <NodeKind kind, typename Entity>
490-
static std::string computeContextForProvidedEntity(Entity);
491-
492543
DependencyKey correspondingImplementation() const {
493544
return withAspect(DeclAspect::implementation);
494545
}
@@ -497,10 +548,6 @@ class DependencyKey {
497548
return DependencyKey(kind, aspect, context, name);
498549
}
499550

500-
/// Given some type of provided entity compute the name field of the key.
501-
template <NodeKind kind, typename Entity>
502-
static std::string computeNameForProvidedEntity(Entity);
503-
504551
static DependencyKey createKeyForWholeSourceFile(DeclAspect,
505552
StringRef swiftDeps);
506553

lib/AST/FineGrainedDependencies.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,9 @@ std::string DependencyKey::demangleTypeAsContext(StringRef s) {
186186
DependencyKey DependencyKey::createKeyForWholeSourceFile(DeclAspect aspect,
187187
StringRef swiftDeps) {
188188
assert(!swiftDeps.empty());
189-
const std::string context = DependencyKey::computeContextForProvidedEntity<
190-
NodeKind::sourceFileProvide>(swiftDeps);
191-
const std::string name =
192-
DependencyKey::computeNameForProvidedEntity<NodeKind::sourceFileProvide>(
193-
swiftDeps);
194-
return DependencyKey(NodeKind::sourceFileProvide, aspect, context, name);
189+
return DependencyKey::Builder(NodeKind::sourceFileProvide, aspect)
190+
.withName(swiftDeps)
191+
.build();
195192
}
196193

197194
//==============================================================================

0 commit comments

Comments
 (0)