[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; Type getUnderlyingType() const;
void setUnderlyingType(Type type); 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. /// For generic typealiases, return the unbound generic type.
UnboundGenericType *getUnboundGenericType() const; UnboundGenericType *getUnboundGenericType() const;

View File

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

View File

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

View File

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

View File

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