AST: Refactor HasImplementationOnlyImportsRequest to handle querying for imports with any ImportFlag.

This commit is contained in:
Allan Shortlidge
2022-08-10 16:27:38 -07:00
parent 0d94fb16d3
commit 7106c60e5a
6 changed files with 83 additions and 15 deletions

View File

@@ -94,6 +94,8 @@ enum class ImportFlags {
/// \see ImportFlags /// \see ImportFlags
using ImportOptions = OptionSet<ImportFlags>; using ImportOptions = OptionSet<ImportFlags>;
void simple_display(llvm::raw_ostream &out, ImportOptions options);
// MARK: - Import Paths // MARK: - Import Paths
namespace detail { namespace detail {

View File

@@ -174,6 +174,16 @@ private:
ParserStatePtr DelayedParserState = ParserStatePtr DelayedParserState =
ParserStatePtr(/*ptr*/ nullptr, /*deleter*/ nullptr); ParserStatePtr(/*ptr*/ nullptr, /*deleter*/ nullptr);
friend class HasImportsMatchingFlagRequest;
/// Indicates which import options have a valid caches. Storage for
/// \c HasImportsMatchingFlagRequest.
ImportOptions validCachedImportOptions;
/// The cached computation of which import flags are present in the file.
/// Storage for \c HasImportsMatchingFlagRequest.
ImportOptions cachedImportOptions;
friend ASTContext; friend ASTContext;
public: public:

View File

@@ -3202,21 +3202,24 @@ public:
bool isCached() const { return true; } bool isCached() const { return true; }
}; };
/// Checks whether a file performs an implementation-only import. /// Checks whether a file contains any import declarations with the given flag.
class HasImplementationOnlyImportsRequest class HasImportsMatchingFlagRequest
: public SimpleRequest<HasImplementationOnlyImportsRequest, : public SimpleRequest<HasImportsMatchingFlagRequest,
bool(SourceFile *), RequestFlags::Cached> { bool(SourceFile *, ImportFlags),
RequestFlags::SeparatelyCached> {
public: public:
using SimpleRequest::SimpleRequest; using SimpleRequest::SimpleRequest;
private: private:
friend SimpleRequest; friend SimpleRequest;
bool evaluate(Evaluator &evaluator, SourceFile *SF) const; bool evaluate(Evaluator &evaluator, SourceFile *SF, ImportFlags flag) const;
public: public:
// Cached. // Cached.
bool isCached() const { return true; } bool isCached() const { return true; }
Optional<bool> getCachedResult() const;
void cacheResult(bool value) const;
}; };
/// Get the library level of a module. /// Get the library level of a module.

View File

@@ -169,8 +169,8 @@ SWIFT_REQUEST(TypeChecker, HasCircularInheritedProtocolsRequest,
bool(ProtocolDecl *), Cached, NoLocationInfo) bool(ProtocolDecl *), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, HasCircularRawValueRequest, SWIFT_REQUEST(TypeChecker, HasCircularRawValueRequest,
bool(EnumDecl *), Cached, NoLocationInfo) bool(EnumDecl *), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, HasImplementationOnlyImportsRequest, SWIFT_REQUEST(TypeChecker, HasImportsMatchingFlagRequest,
bool(SourceFile *), Cached, NoLocationInfo) bool(SourceFile *, ImportOptions), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, ModuleLibraryLevelRequest, SWIFT_REQUEST(TypeChecker, ModuleLibraryLevelRequest,
LibraryLevel(ModuleDecl *), Cached, NoLocationInfo) LibraryLevel(ModuleDecl *), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest, SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,

View File

@@ -2389,19 +2389,66 @@ void SourceFile::setImportUsedPreconcurrency(
PreconcurrencyImportsUsed.insert(import); PreconcurrencyImportsUsed.insert(import);
} }
bool HasImplementationOnlyImportsRequest::evaluate(Evaluator &evaluator, bool HasImportsMatchingFlagRequest::evaluate(Evaluator &evaluator,
SourceFile *SF) const { SourceFile *SF,
return llvm::any_of(SF->getImports(), ImportFlags flag) const {
[](AttributedImport<ImportedModule> desc) { for (auto desc : SF->getImports()) {
return desc.options.contains(ImportFlags::ImplementationOnly); if (desc.options.contains(flag))
}); return true;
}
return false;
}
Optional<bool> HasImportsMatchingFlagRequest::getCachedResult() const {
SourceFile *sourceFile = std::get<0>(getStorage());
ImportFlags flag = std::get<1>(getStorage());
if (sourceFile->validCachedImportOptions.contains(flag))
return sourceFile->cachedImportOptions.contains(flag);
return None;
}
void HasImportsMatchingFlagRequest::cacheResult(bool value) const {
SourceFile *sourceFile = std::get<0>(getStorage());
ImportFlags flag = std::get<1>(getStorage());
sourceFile->validCachedImportOptions |= flag;
if (value)
sourceFile->cachedImportOptions |= flag;
}
void swift::simple_display(llvm::raw_ostream &out, ImportOptions options) {
using Flag = std::pair<ImportFlags, StringRef>;
Flag possibleFlags[] = {
#define FLAG(Name) {ImportFlags::Name, #Name},
FLAG(Exported)
FLAG(Testable)
FLAG(PrivateImport)
FLAG(ImplementationOnly)
FLAG(SPIAccessControl)
FLAG(Preconcurrency)
FLAG(WeakLinked)
FLAG(Reserved)
#undef FLAG
};
auto flagsToPrint = llvm::make_filter_range(
possibleFlags, [&](Flag flag) { return options & flag.first; });
out << "{ ";
interleave(
flagsToPrint, [&](Flag flag) { out << flag.second; },
[&] { out << ", "; });
out << " }";
} }
bool SourceFile::hasImplementationOnlyImports() const { bool SourceFile::hasImplementationOnlyImports() const {
auto &ctx = getASTContext(); auto &ctx = getASTContext();
auto *mutableThis = const_cast<SourceFile *>(this); auto *mutableThis = const_cast<SourceFile *>(this);
return evaluateOrDefault( return evaluateOrDefault(ctx.evaluator,
ctx.evaluator, HasImplementationOnlyImportsRequest{mutableThis}, false); HasImportsMatchingFlagRequest{
mutableThis, ImportFlags::ImplementationOnly},
false);
} }
bool SourceFile::hasTestableOrPrivateImport( bool SourceFile::hasTestableOrPrivateImport(

View File

@@ -1474,6 +1474,12 @@ void swift::simple_display(llvm::raw_ostream &out,
out << ")"; out << ")";
} }
if (import.options.contains(ImportFlags::Preconcurrency))
out << " preconcurrency";
if (import.options.contains(ImportFlags::WeakLinked))
out << " weak-linked";
out << " ]"; out << " ]";
} }