Skip to content

Requestify hasImplementationOnlyImports #31181

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
Apr 21, 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
13 changes: 6 additions & 7 deletions include/swift/AST/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ class SourceFile final : public FileUnit {
/// May be -1, to indicate no association with a buffer.
int BufferID;

/// Does this source file have any implementation-only imports?
/// If not, we can fast-path module checks.
bool HasImplementationOnlyImports = false;

/// The parsing options for the file.
ParsingOptions ParsingOpts;

Expand Down Expand Up @@ -333,6 +329,9 @@ class SourceFile final : public FileUnit {

~SourceFile();

/// Retrieve an immutable view of the source file's imports.
ArrayRef<ImportedModuleDesc> getImports() const { return *Imports; }

/// Set the imports for this source file. This gets called by import
/// resolution.
void setImports(ArrayRef<ImportedModuleDesc> imports);
Expand All @@ -350,9 +349,9 @@ class SourceFile final : public FileUnit {
hasTestableOrPrivateImport(AccessLevel accessLevel, const ValueDecl *ofDecl,
ImportQueryKind kind = TestableAndPrivate) const;

bool hasImplementationOnlyImports() const {
return HasImplementationOnlyImports;
}
/// Does this source file have any implementation-only imports?
/// If not, we can fast-path module checks.
bool hasImplementationOnlyImports() const;

bool isImportedImplementationOnly(const ModuleDecl *module) const;

Expand Down
17 changes: 17 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -2384,6 +2384,23 @@ class ModuleImplicitImportsRequest
bool isCached() const { return true; }
};

/// Checks whether a file performs an implementation-only import.
class HasImplementationOnlyImportsRequest
: public SimpleRequest<HasImplementationOnlyImportsRequest,
bool(SourceFile *), RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

bool evaluate(Evaluator &evaluator, SourceFile *SF) const;

public:
// Cached.
bool isCached() const { return true; }
};

// Allow AnyValue to compare two Type values, even though Type doesn't
// support ==.
template<>
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ SWIFT_REQUEST(TypeChecker, HasDynamicMemberLookupAttributeRequest,
bool(CanType), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, HasDynamicCallableAttributeRequest,
bool(CanType), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, HasImplementationOnlyImportsRequest,
bool(SourceFile *), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
GenericSignature (ModuleDecl *, GenericSignatureImpl *,
GenericParamSource,
Expand Down
23 changes: 15 additions & 8 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2042,15 +2042,22 @@ void SourceFile::print(ASTPrinter &Printer, const PrintOptions &PO) {
void SourceFile::setImports(ArrayRef<ImportedModuleDesc> imports) {
assert(!Imports && "Already computed imports");
Imports = getASTContext().AllocateCopy(imports);
}

// Update the HasImplementationOnlyImports flag.
// TODO: Requestify this.
if (!HasImplementationOnlyImports) {
for (auto &desc : imports) {
if (desc.importOptions.contains(ImportFlags::ImplementationOnly))
HasImplementationOnlyImports = true;
}
}
bool HasImplementationOnlyImportsRequest::evaluate(Evaluator &evaluator,
SourceFile *SF) const {
using ModuleDesc = SourceFile::ImportedModuleDesc;
return llvm::any_of(SF->getImports(), [](ModuleDesc desc) {
return desc.importOptions.contains(
SourceFile::ImportFlags::ImplementationOnly);
});
}

bool SourceFile::hasImplementationOnlyImports() const {
auto &ctx = getASTContext();
auto *mutableThis = const_cast<SourceFile *>(this);
return evaluateOrDefault(
ctx.evaluator, HasImplementationOnlyImportsRequest{mutableThis}, false);
}

bool SourceFile::hasTestableOrPrivateImport(
Expand Down