Skip to content

AST: Request-ify ValueDecl::findImport() #72758

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 2, 2024
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
5 changes: 5 additions & 0 deletions include/swift/AST/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ class SourceFile final : public FileUnit {
ImportedUnderlyingModule = module;
}

/// Finds the import declaration that effectively imports a given module in
/// this source file.
std::optional<AttributedImport<ImportedModule>>
findImport(const ModuleDecl *mod) const;

/// Whether the given import has used @preconcurrency.
bool hasImportUsedPreconcurrency(
AttributedImport<ImportedModule> import) const;
Expand Down
21 changes: 21 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -4845,6 +4845,27 @@ class ObjCRequirementMapRequest
bool isCached() const { return true; }
};

/// Finds the import declaration that effectively imports a given module in a
/// source file.
class ImportDeclRequest
: public SimpleRequest<ImportDeclRequest,
std::optional<AttributedImport<ImportedModule>>(
const SourceFile *sf, const ModuleDecl *mod),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

std::optional<AttributedImport<ImportedModule>>
evaluate(Evaluator &evaluator, const SourceFile *sf,
const ModuleDecl *mod) const;

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

#define SWIFT_TYPEID_ZONE TypeChecker
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
#include "swift/Basic/DefineTypeIDZone.h"
Expand Down
5 changes: 4 additions & 1 deletion include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,7 @@ SWIFT_REQUEST(TypeChecker, LocalTypeDeclsRequest,
SWIFT_REQUEST(TypeChecker, ObjCRequirementMapRequest,
ObjCRequirementMap(const ProtocolDecl *proto),
Cached, NoLocationInfo)

SWIFT_REQUEST(TypeChecker, ImportDeclRequest,
std::optional<AttributedImport<ImportedModule>>(
const SourceFile *sf, const ModuleDecl *mod),
Cached, NoLocationInfo)
19 changes: 1 addition & 18 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3851,24 +3851,7 @@ ValueDecl::findImport(const DeclContext *fromDC) {
if (!fromSourceFile)
return std::nullopt;

// Look to see if the owning module was directly imported.
for (const auto &import : fromSourceFile->getImports()) {
if (import.module.importedModule == module)
return import;
}

// Now look for transitive imports.
auto &importCache = getASTContext().getImportCache();
for (const auto &import : fromSourceFile->getImports()) {
auto &importSet = importCache.getImportSet(import.module.importedModule);
for (const auto &transitive : importSet.getTransitiveImports()) {
if (transitive.importedModule == module) {
return import;
}
}
}

return std::nullopt;
return fromSourceFile->findImport(module);
}

bool ValueDecl::isProtocolRequirement() const {
Expand Down
32 changes: 32 additions & 0 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,38 @@ SourceFile::setImports(ArrayRef<AttributedImport<ImportedModule>> imports) {
Imports = getASTContext().AllocateCopy(imports);
}

std::optional<AttributedImport<ImportedModule>>
SourceFile::findImport(const ModuleDecl *module) const {
return evaluateOrDefault(getASTContext().evaluator,
ImportDeclRequest{this, module}, std::nullopt);
}

std::optional<AttributedImport<ImportedModule>>
ImportDeclRequest::evaluate(Evaluator &evaluator, const SourceFile *sf,
const ModuleDecl *module) const {
auto &ctx = sf->getASTContext();
auto imports = sf->getImports();

// Look to see if the owning module was directly imported.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an overload of ImportCache::getImportSet() that takes an ArrayRef. Maybe it can replace this whole thing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was originally located in TypeCheckConcurrency, so there is a high chance that it’s possible to dedupe…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like ImportSet is a flat set of imports, so the context of which top level import each transitive import comes from is lost. Correct me if I'm missing something but I don't see how that can be used to implement this request that needs to return the specific import from which a module is reachable.

for (const auto &import : imports) {
if (import.module.importedModule == module)
return import;
}

// Now look for transitive imports.
auto &importCache = ctx.getImportCache();
for (const auto &import : imports) {
auto &importSet = importCache.getImportSet(import.module.importedModule);
for (const auto &transitive : importSet.getTransitiveImports()) {
if (transitive.importedModule == module) {
return import;
}
}
}

return std::nullopt;
}

bool SourceFile::hasImportUsedPreconcurrency(
AttributedImport<ImportedModule> import) const {
return PreconcurrencyImportsUsed.count(import) != 0;
Expand Down