Skip to content

Commit bb4d076

Browse files
authored
Requestify hasImplementationOnlyImports (#31181)
Requestify hasImplementationOnlyImports
2 parents 102bc6a + 76ec21f commit bb4d076

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

include/swift/AST/SourceFile.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,6 @@ class SourceFile final : public FileUnit {
157157
/// May be -1, to indicate no association with a buffer.
158158
int BufferID;
159159

160-
/// Does this source file have any implementation-only imports?
161-
/// If not, we can fast-path module checks.
162-
bool HasImplementationOnlyImports = false;
163-
164160
/// The parsing options for the file.
165161
ParsingOptions ParsingOpts;
166162

@@ -333,6 +329,9 @@ class SourceFile final : public FileUnit {
333329

334330
~SourceFile();
335331

332+
/// Retrieve an immutable view of the source file's imports.
333+
ArrayRef<ImportedModuleDesc> getImports() const { return *Imports; }
334+
336335
/// Set the imports for this source file. This gets called by import
337336
/// resolution.
338337
void setImports(ArrayRef<ImportedModuleDesc> imports);
@@ -350,9 +349,9 @@ class SourceFile final : public FileUnit {
350349
hasTestableOrPrivateImport(AccessLevel accessLevel, const ValueDecl *ofDecl,
351350
ImportQueryKind kind = TestableAndPrivate) const;
352351

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

357356
bool isImportedImplementationOnly(const ModuleDecl *module) const;
358357

include/swift/AST/TypeCheckRequests.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,6 +2384,23 @@ class ModuleImplicitImportsRequest
23842384
bool isCached() const { return true; }
23852385
};
23862386

2387+
/// Checks whether a file performs an implementation-only import.
2388+
class HasImplementationOnlyImportsRequest
2389+
: public SimpleRequest<HasImplementationOnlyImportsRequest,
2390+
bool(SourceFile *), RequestFlags::Cached> {
2391+
public:
2392+
using SimpleRequest::SimpleRequest;
2393+
2394+
private:
2395+
friend SimpleRequest;
2396+
2397+
bool evaluate(Evaluator &evaluator, SourceFile *SF) const;
2398+
2399+
public:
2400+
// Cached.
2401+
bool isCached() const { return true; }
2402+
};
2403+
23872404
// Allow AnyValue to compare two Type values, even though Type doesn't
23882405
// support ==.
23892406
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ SWIFT_REQUEST(TypeChecker, HasDynamicMemberLookupAttributeRequest,
8989
bool(CanType), Cached, NoLocationInfo)
9090
SWIFT_REQUEST(TypeChecker, HasDynamicCallableAttributeRequest,
9191
bool(CanType), Cached, NoLocationInfo)
92+
SWIFT_REQUEST(TypeChecker, HasImplementationOnlyImportsRequest,
93+
bool(SourceFile *), Cached, NoLocationInfo)
9294
SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
9395
GenericSignature (ModuleDecl *, GenericSignatureImpl *,
9496
GenericParamSource,

lib/AST/Module.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,15 +2042,22 @@ void SourceFile::print(ASTPrinter &Printer, const PrintOptions &PO) {
20422042
void SourceFile::setImports(ArrayRef<ImportedModuleDesc> imports) {
20432043
assert(!Imports && "Already computed imports");
20442044
Imports = getASTContext().AllocateCopy(imports);
2045+
}
20452046

2046-
// Update the HasImplementationOnlyImports flag.
2047-
// TODO: Requestify this.
2048-
if (!HasImplementationOnlyImports) {
2049-
for (auto &desc : imports) {
2050-
if (desc.importOptions.contains(ImportFlags::ImplementationOnly))
2051-
HasImplementationOnlyImports = true;
2052-
}
2053-
}
2047+
bool HasImplementationOnlyImportsRequest::evaluate(Evaluator &evaluator,
2048+
SourceFile *SF) const {
2049+
using ModuleDesc = SourceFile::ImportedModuleDesc;
2050+
return llvm::any_of(SF->getImports(), [](ModuleDesc desc) {
2051+
return desc.importOptions.contains(
2052+
SourceFile::ImportFlags::ImplementationOnly);
2053+
});
2054+
}
2055+
2056+
bool SourceFile::hasImplementationOnlyImports() const {
2057+
auto &ctx = getASTContext();
2058+
auto *mutableThis = const_cast<SourceFile *>(this);
2059+
return evaluateOrDefault(
2060+
ctx.evaluator, HasImplementationOnlyImportsRequest{mutableThis}, false);
20542061
}
20552062

20562063
bool SourceFile::hasTestableOrPrivateImport(

0 commit comments

Comments
 (0)