[AST] Remove SourceFile::addImports

Temporarily replace with `SourceFile::setImports`
until import resolution is requestified. Now
imports are only set once for a given SourceFile.

Because we're now asserting in more places that
import resolution must have run before querying
imports, this commit also adds
`getCachedUnderlyingType` to TypeAliasDecl to stop
the ASTDumper from trying to query imports for a
-dump-parse invocation.
This commit is contained in:
Hamish Knight
2020-04-14 08:32:02 -07:00
parent b500f371fe
commit 92103a7c63
5 changed files with 25 additions and 25 deletions

View File

@@ -3032,6 +3032,10 @@ public:
Type getUnderlyingType() const;
void setUnderlyingType(Type type);
/// Returns the interface type of the underlying type if computed, null
/// otherwise. Should only be used for dumping.
Type getCachedUnderlyingType() const { return UnderlyingTy.getType(); }
/// For generic typealiases, return the unbound generic type.
UnboundGenericType *getUnboundGenericType() const;

View File

@@ -121,8 +121,8 @@ private:
/// This is the list of modules that are imported by this module.
///
/// This is filled in by the import resolution phase.
ArrayRef<ImportedModuleDesc> Imports;
/// This is \c None until it is filled in by the import resolution phase.
Optional<ArrayRef<ImportedModuleDesc>> Imports;
/// A unique identifier representing this file; used to mark private decls
/// within the file to keep them from conflicting with other files in the
@@ -333,7 +333,9 @@ public:
~SourceFile();
void addImports(ArrayRef<ImportedModuleDesc> IM);
/// Set the imports for this source file. This gets called by import
/// resolution.
void setImports(ArrayRef<ImportedModuleDesc> imports);
enum ImportQueryKind {
/// Return the results for testable or private imports.

View File

@@ -631,7 +631,7 @@ namespace {
void visitTypeAliasDecl(TypeAliasDecl *TAD) {
printCommon(TAD, "typealias");
PrintWithColorRAII(OS, TypeColor) << " type='";
if (auto underlying = TAD->getUnderlyingType()) {
if (auto underlying = TAD->getCachedUnderlyingType()) {
PrintWithColorRAII(OS, TypeColor)
<< underlying.getString();
} else {

View File

@@ -1103,7 +1103,7 @@ public:
/// Only intended for use by lookupOperatorDeclForName.
static ArrayRef<SourceFile::ImportedModuleDesc>
getImportsForSourceFile(const SourceFile &SF) {
return SF.Imports;
return *SF.Imports;
}
};
@@ -1408,7 +1408,10 @@ SourceFile::getImportedModules(SmallVectorImpl<ModuleDecl::ImportedModule> &modu
// We currently handle this for a direct import from the overlay, but not when
// it happens through other imports.
assert(filter && "no imports requested?");
for (auto desc : Imports) {
if (!Imports)
return;
for (auto desc : *Imports) {
ModuleDecl::ImportFilter requiredFilter;
if (desc.importOptions.contains(ImportFlags::Exported))
requiredFilter |= ModuleDecl::ImportFilterKind::Public;
@@ -2036,23 +2039,14 @@ void SourceFile::print(ASTPrinter &Printer, const PrintOptions &PO) {
}
}
void SourceFile::addImports(ArrayRef<ImportedModuleDesc> IM) {
if (IM.empty())
return;
ASTContext &ctx = getASTContext();
auto newBuf =
ctx.AllocateUninitialized<ImportedModuleDesc>(Imports.size() + IM.size());
auto iter = newBuf.begin();
iter = std::uninitialized_copy(Imports.begin(), Imports.end(), iter);
iter = std::uninitialized_copy(IM.begin(), IM.end(), iter);
assert(iter == newBuf.end());
Imports = newBuf;
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 : IM) {
for (auto &desc : imports) {
if (desc.importOptions.contains(ImportFlags::ImplementationOnly))
HasImplementationOnlyImports = true;
}
@@ -2070,7 +2064,7 @@ bool SourceFile::hasTestableOrPrivateImport(
// filename does not need to match (and we don't serialize it for such
// decls).
return std::any_of(
Imports.begin(), Imports.end(),
Imports->begin(), Imports->end(),
[module, queryKind](ImportedModuleDesc desc) -> bool {
if (queryKind == ImportQueryKind::TestableAndPrivate)
return desc.module.second == module &&
@@ -2112,7 +2106,7 @@ bool SourceFile::hasTestableOrPrivateImport(
if (filename.empty())
return false;
return std::any_of(Imports.begin(), Imports.end(),
return std::any_of(Imports->begin(), Imports->end(),
[module, filename](ImportedModuleDesc desc) -> bool {
return desc.module.second == module &&
desc.importOptions.contains(
@@ -2130,7 +2124,7 @@ bool SourceFile::isImportedImplementationOnly(const ModuleDecl *module) const {
auto &imports = getASTContext().getImportCache();
// Look at the imports of this source file.
for (auto &desc : Imports) {
for (auto &desc : *Imports) {
// Ignore implementation-only imports.
if (desc.importOptions.contains(ImportFlags::ImplementationOnly))
continue;
@@ -2147,7 +2141,7 @@ bool SourceFile::isImportedImplementationOnly(const ModuleDecl *module) const {
void SourceFile::lookupImportedSPIGroups(const ModuleDecl *importedModule,
SmallVectorImpl<Identifier> &spiGroups) const {
for (auto &import : Imports) {
for (auto &import : *Imports) {
if (import.importOptions.contains(ImportFlags::SPIAccessControl) &&
importedModule == std::get<ModuleDecl*>(import.module)) {
auto importedSpis = import.spiGroups;

View File

@@ -288,7 +288,7 @@ void swift::performImportResolution(SourceFile &SF) {
for (auto D : SF.getTopLevelDecls())
resolver.visit(D);
SF.addImports(resolver.getFinishedImports());
SF.setImports(resolver.getFinishedImports());
SF.ASTStage = SourceFile::ImportsResolved;
verify(SF);