Skip to content

[NFC] Convert the Templates Computing DependencyKeys to the Builder Pattern #36096

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 1 commit into from
Feb 23, 2021
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
63 changes: 55 additions & 8 deletions include/swift/AST/FineGrainedDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef SWIFT_AST_FINE_GRAINED_DEPENDENCIES_H
#define SWIFT_AST_FINE_GRAINED_DEPENDENCIES_H

#include "swift/AST/EvaluatorDependencies.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/Fingerprint.h"
#include "swift/Basic/LLVM.h"
Expand Down Expand Up @@ -57,11 +58,14 @@
//==============================================================================

namespace swift {
class Decl;
class DependencyTracker;
class DiagnosticEngine;
class FrontendOptions;
class ModuleDecl;
class SourceFile;
class NominalTypeDecl;
class ValueDecl;

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

public:
class Builder {
private:
const NodeKind kind;
const DeclAspect aspect;
const NominalTypeDecl *context;
StringRef name;

private:
// A private copy constructor so our clients are forced to use the
// move-only builder interface.
explicit Builder(NodeKind kind, DeclAspect aspect,
const NominalTypeDecl *context, StringRef name)
: kind(kind), aspect(aspect), context(context), name(name) {}

public:
/// Creates a DependencyKey::Builder from the given \p kind and \p aspect
/// with a \c null context and empty name.
explicit Builder(NodeKind kind, DeclAspect aspect)
: kind(kind), aspect(aspect), context(nullptr), name("") {}

public:
/// Consumes this builder and returns a dependency key created from its
/// data.
DependencyKey build() &&;

public:
/// Extracts the data from the given \p ref into a this builder.
Builder fromReference(const evaluator::DependencyCollector::Reference &ref);

public:
/// Extracts the context data from the given declaration, if any.
Builder withContext(const Decl *D) &&;
/// Extracts the context data from the given decl-member pair, if any.
Builder withContext(std::pair<const NominalTypeDecl *, const ValueDecl *>
holderAndMember) &&;

public:
/// Copies the name data for the given swiftdeps file into this builder.
Builder withName(StringRef swiftDeps) &&;
/// Copies the name of the given declaration into this builder, if any.
Builder withName(const Decl *decl) &&;
/// Extracts the name from the given decl-member pair, if any.
Builder withName(std::pair<const NominalTypeDecl *, const ValueDecl *>
holderAndMember) &&;

private:
static StringRef getTopLevelName(const Decl *decl);
};

private:
NodeKind kind;
DeclAspect aspect;
/// The mangled context type name of the holder for \ref potentialMember, \ref
Expand Down Expand Up @@ -485,10 +540,6 @@ class DependencyKey {
template <NodeKind kind, typename Entity>
static DependencyKey createForProvidedEntityInterface(Entity);

/// Given some type of provided entity compute the context field of the key.
template <NodeKind kind, typename Entity>
static std::string computeContextForProvidedEntity(Entity);

DependencyKey correspondingImplementation() const {
return withAspect(DeclAspect::implementation);
}
Expand All @@ -497,10 +548,6 @@ class DependencyKey {
return DependencyKey(kind, aspect, context, name);
}

/// Given some type of provided entity compute the name field of the key.
template <NodeKind kind, typename Entity>
static std::string computeNameForProvidedEntity(Entity);

static DependencyKey createKeyForWholeSourceFile(DeclAspect,
StringRef swiftDeps);

Expand Down
9 changes: 3 additions & 6 deletions lib/AST/FineGrainedDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,9 @@ std::string DependencyKey::demangleTypeAsContext(StringRef s) {
DependencyKey DependencyKey::createKeyForWholeSourceFile(DeclAspect aspect,
StringRef swiftDeps) {
assert(!swiftDeps.empty());
const std::string context = DependencyKey::computeContextForProvidedEntity<
NodeKind::sourceFileProvide>(swiftDeps);
const std::string name =
DependencyKey::computeNameForProvidedEntity<NodeKind::sourceFileProvide>(
swiftDeps);
return DependencyKey(NodeKind::sourceFileProvide, aspect, context, name);
return DependencyKey::Builder(NodeKind::sourceFileProvide, aspect)
.withName(swiftDeps)
.build();
}

//==============================================================================
Expand Down
Loading