Skip to content

Commit 7106c60

Browse files
committed
AST: Refactor HasImplementationOnlyImportsRequest to handle querying for imports with any ImportFlag.
1 parent 0d94fb1 commit 7106c60

File tree

6 files changed

+83
-15
lines changed

6 files changed

+83
-15
lines changed

include/swift/AST/Import.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ enum class ImportFlags {
9494
/// \see ImportFlags
9595
using ImportOptions = OptionSet<ImportFlags>;
9696

97+
void simple_display(llvm::raw_ostream &out, ImportOptions options);
98+
9799
// MARK: - Import Paths
98100

99101
namespace detail {

include/swift/AST/SourceFile.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ class SourceFile final : public FileUnit {
174174
ParserStatePtr DelayedParserState =
175175
ParserStatePtr(/*ptr*/ nullptr, /*deleter*/ nullptr);
176176

177+
friend class HasImportsMatchingFlagRequest;
178+
179+
/// Indicates which import options have a valid caches. Storage for
180+
/// \c HasImportsMatchingFlagRequest.
181+
ImportOptions validCachedImportOptions;
182+
183+
/// The cached computation of which import flags are present in the file.
184+
/// Storage for \c HasImportsMatchingFlagRequest.
185+
ImportOptions cachedImportOptions;
186+
177187
friend ASTContext;
178188

179189
public:

include/swift/AST/TypeCheckRequests.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,21 +3202,24 @@ class ModuleImplicitImportsRequest
32023202
bool isCached() const { return true; }
32033203
};
32043204

3205-
/// Checks whether a file performs an implementation-only import.
3206-
class HasImplementationOnlyImportsRequest
3207-
: public SimpleRequest<HasImplementationOnlyImportsRequest,
3208-
bool(SourceFile *), RequestFlags::Cached> {
3205+
/// Checks whether a file contains any import declarations with the given flag.
3206+
class HasImportsMatchingFlagRequest
3207+
: public SimpleRequest<HasImportsMatchingFlagRequest,
3208+
bool(SourceFile *, ImportFlags),
3209+
RequestFlags::SeparatelyCached> {
32093210
public:
32103211
using SimpleRequest::SimpleRequest;
32113212

32123213
private:
32133214
friend SimpleRequest;
32143215

3215-
bool evaluate(Evaluator &evaluator, SourceFile *SF) const;
3216+
bool evaluate(Evaluator &evaluator, SourceFile *SF, ImportFlags flag) const;
32163217

32173218
public:
32183219
// Cached.
32193220
bool isCached() const { return true; }
3221+
Optional<bool> getCachedResult() const;
3222+
void cacheResult(bool value) const;
32203223
};
32213224

32223225
/// Get the library level of a module.

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ SWIFT_REQUEST(TypeChecker, HasCircularInheritedProtocolsRequest,
169169
bool(ProtocolDecl *), Cached, NoLocationInfo)
170170
SWIFT_REQUEST(TypeChecker, HasCircularRawValueRequest,
171171
bool(EnumDecl *), Cached, NoLocationInfo)
172-
SWIFT_REQUEST(TypeChecker, HasImplementationOnlyImportsRequest,
173-
bool(SourceFile *), Cached, NoLocationInfo)
172+
SWIFT_REQUEST(TypeChecker, HasImportsMatchingFlagRequest,
173+
bool(SourceFile *, ImportOptions), Cached, NoLocationInfo)
174174
SWIFT_REQUEST(TypeChecker, ModuleLibraryLevelRequest,
175175
LibraryLevel(ModuleDecl *), Cached, NoLocationInfo)
176176
SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,

lib/AST/Module.cpp

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,19 +2389,66 @@ void SourceFile::setImportUsedPreconcurrency(
23892389
PreconcurrencyImportsUsed.insert(import);
23902390
}
23912391

2392-
bool HasImplementationOnlyImportsRequest::evaluate(Evaluator &evaluator,
2393-
SourceFile *SF) const {
2394-
return llvm::any_of(SF->getImports(),
2395-
[](AttributedImport<ImportedModule> desc) {
2396-
return desc.options.contains(ImportFlags::ImplementationOnly);
2397-
});
2392+
bool HasImportsMatchingFlagRequest::evaluate(Evaluator &evaluator,
2393+
SourceFile *SF,
2394+
ImportFlags flag) const {
2395+
for (auto desc : SF->getImports()) {
2396+
if (desc.options.contains(flag))
2397+
return true;
2398+
}
2399+
return false;
2400+
}
2401+
2402+
Optional<bool> HasImportsMatchingFlagRequest::getCachedResult() const {
2403+
SourceFile *sourceFile = std::get<0>(getStorage());
2404+
ImportFlags flag = std::get<1>(getStorage());
2405+
if (sourceFile->validCachedImportOptions.contains(flag))
2406+
return sourceFile->cachedImportOptions.contains(flag);
2407+
2408+
return None;
2409+
}
2410+
2411+
void HasImportsMatchingFlagRequest::cacheResult(bool value) const {
2412+
SourceFile *sourceFile = std::get<0>(getStorage());
2413+
ImportFlags flag = std::get<1>(getStorage());
2414+
2415+
sourceFile->validCachedImportOptions |= flag;
2416+
if (value)
2417+
sourceFile->cachedImportOptions |= flag;
2418+
}
2419+
2420+
void swift::simple_display(llvm::raw_ostream &out, ImportOptions options) {
2421+
using Flag = std::pair<ImportFlags, StringRef>;
2422+
Flag possibleFlags[] = {
2423+
#define FLAG(Name) {ImportFlags::Name, #Name},
2424+
FLAG(Exported)
2425+
FLAG(Testable)
2426+
FLAG(PrivateImport)
2427+
FLAG(ImplementationOnly)
2428+
FLAG(SPIAccessControl)
2429+
FLAG(Preconcurrency)
2430+
FLAG(WeakLinked)
2431+
FLAG(Reserved)
2432+
#undef FLAG
2433+
};
2434+
2435+
auto flagsToPrint = llvm::make_filter_range(
2436+
possibleFlags, [&](Flag flag) { return options & flag.first; });
2437+
2438+
out << "{ ";
2439+
interleave(
2440+
flagsToPrint, [&](Flag flag) { out << flag.second; },
2441+
[&] { out << ", "; });
2442+
out << " }";
23982443
}
23992444

24002445
bool SourceFile::hasImplementationOnlyImports() const {
24012446
auto &ctx = getASTContext();
24022447
auto *mutableThis = const_cast<SourceFile *>(this);
2403-
return evaluateOrDefault(
2404-
ctx.evaluator, HasImplementationOnlyImportsRequest{mutableThis}, false);
2448+
return evaluateOrDefault(ctx.evaluator,
2449+
HasImportsMatchingFlagRequest{
2450+
mutableThis, ImportFlags::ImplementationOnly},
2451+
false);
24052452
}
24062453

24072454
bool SourceFile::hasTestableOrPrivateImport(

lib/AST/TypeCheckRequests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,12 @@ void swift::simple_display(llvm::raw_ostream &out,
14741474
out << ")";
14751475
}
14761476

1477+
if (import.options.contains(ImportFlags::Preconcurrency))
1478+
out << " preconcurrency";
1479+
1480+
if (import.options.contains(ImportFlags::WeakLinked))
1481+
out << " weak-linked";
1482+
14771483
out << " ]";
14781484
}
14791485

0 commit comments

Comments
 (0)