Skip to content

Commit 92103a7

Browse files
committed
[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.
1 parent b500f37 commit 92103a7

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,6 +3032,10 @@ class TypeAliasDecl : public GenericTypeDecl {
30323032
Type getUnderlyingType() const;
30333033
void setUnderlyingType(Type type);
30343034

3035+
/// Returns the interface type of the underlying type if computed, null
3036+
/// otherwise. Should only be used for dumping.
3037+
Type getCachedUnderlyingType() const { return UnderlyingTy.getType(); }
3038+
30353039
/// For generic typealiases, return the unbound generic type.
30363040
UnboundGenericType *getUnboundGenericType() const;
30373041

include/swift/AST/SourceFile.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ class SourceFile final : public FileUnit {
121121

122122
/// This is the list of modules that are imported by this module.
123123
///
124-
/// This is filled in by the import resolution phase.
125-
ArrayRef<ImportedModuleDesc> Imports;
124+
/// This is \c None until it is filled in by the import resolution phase.
125+
Optional<ArrayRef<ImportedModuleDesc>> Imports;
126126

127127
/// A unique identifier representing this file; used to mark private decls
128128
/// within the file to keep them from conflicting with other files in the
@@ -333,7 +333,9 @@ class SourceFile final : public FileUnit {
333333

334334
~SourceFile();
335335

336-
void addImports(ArrayRef<ImportedModuleDesc> IM);
336+
/// Set the imports for this source file. This gets called by import
337+
/// resolution.
338+
void setImports(ArrayRef<ImportedModuleDesc> imports);
337339

338340
enum ImportQueryKind {
339341
/// Return the results for testable or private imports.

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ namespace {
631631
void visitTypeAliasDecl(TypeAliasDecl *TAD) {
632632
printCommon(TAD, "typealias");
633633
PrintWithColorRAII(OS, TypeColor) << " type='";
634-
if (auto underlying = TAD->getUnderlyingType()) {
634+
if (auto underlying = TAD->getCachedUnderlyingType()) {
635635
PrintWithColorRAII(OS, TypeColor)
636636
<< underlying.getString();
637637
} else {

lib/AST/Module.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ class SourceFile::Impl {
11031103
/// Only intended for use by lookupOperatorDeclForName.
11041104
static ArrayRef<SourceFile::ImportedModuleDesc>
11051105
getImportsForSourceFile(const SourceFile &SF) {
1106-
return SF.Imports;
1106+
return *SF.Imports;
11071107
}
11081108
};
11091109

@@ -1408,7 +1408,10 @@ SourceFile::getImportedModules(SmallVectorImpl<ModuleDecl::ImportedModule> &modu
14081408
// We currently handle this for a direct import from the overlay, but not when
14091409
// it happens through other imports.
14101410
assert(filter && "no imports requested?");
1411-
for (auto desc : Imports) {
1411+
if (!Imports)
1412+
return;
1413+
1414+
for (auto desc : *Imports) {
14121415
ModuleDecl::ImportFilter requiredFilter;
14131416
if (desc.importOptions.contains(ImportFlags::Exported))
14141417
requiredFilter |= ModuleDecl::ImportFilterKind::Public;
@@ -2036,23 +2039,14 @@ void SourceFile::print(ASTPrinter &Printer, const PrintOptions &PO) {
20362039
}
20372040
}
20382041

2039-
void SourceFile::addImports(ArrayRef<ImportedModuleDesc> IM) {
2040-
if (IM.empty())
2041-
return;
2042-
ASTContext &ctx = getASTContext();
2043-
auto newBuf =
2044-
ctx.AllocateUninitialized<ImportedModuleDesc>(Imports.size() + IM.size());
2045-
2046-
auto iter = newBuf.begin();
2047-
iter = std::uninitialized_copy(Imports.begin(), Imports.end(), iter);
2048-
iter = std::uninitialized_copy(IM.begin(), IM.end(), iter);
2049-
assert(iter == newBuf.end());
2050-
2051-
Imports = newBuf;
2042+
void SourceFile::setImports(ArrayRef<ImportedModuleDesc> imports) {
2043+
assert(!Imports && "Already computed imports");
2044+
Imports = getASTContext().AllocateCopy(imports);
20522045

20532046
// Update the HasImplementationOnlyImports flag.
2047+
// TODO: Requestify this.
20542048
if (!HasImplementationOnlyImports) {
2055-
for (auto &desc : IM) {
2049+
for (auto &desc : imports) {
20562050
if (desc.importOptions.contains(ImportFlags::ImplementationOnly))
20572051
HasImplementationOnlyImports = true;
20582052
}
@@ -2070,7 +2064,7 @@ bool SourceFile::hasTestableOrPrivateImport(
20702064
// filename does not need to match (and we don't serialize it for such
20712065
// decls).
20722066
return std::any_of(
2073-
Imports.begin(), Imports.end(),
2067+
Imports->begin(), Imports->end(),
20742068
[module, queryKind](ImportedModuleDesc desc) -> bool {
20752069
if (queryKind == ImportQueryKind::TestableAndPrivate)
20762070
return desc.module.second == module &&
@@ -2112,7 +2106,7 @@ bool SourceFile::hasTestableOrPrivateImport(
21122106
if (filename.empty())
21132107
return false;
21142108

2115-
return std::any_of(Imports.begin(), Imports.end(),
2109+
return std::any_of(Imports->begin(), Imports->end(),
21162110
[module, filename](ImportedModuleDesc desc) -> bool {
21172111
return desc.module.second == module &&
21182112
desc.importOptions.contains(
@@ -2130,7 +2124,7 @@ bool SourceFile::isImportedImplementationOnly(const ModuleDecl *module) const {
21302124
auto &imports = getASTContext().getImportCache();
21312125

21322126
// Look at the imports of this source file.
2133-
for (auto &desc : Imports) {
2127+
for (auto &desc : *Imports) {
21342128
// Ignore implementation-only imports.
21352129
if (desc.importOptions.contains(ImportFlags::ImplementationOnly))
21362130
continue;
@@ -2147,7 +2141,7 @@ bool SourceFile::isImportedImplementationOnly(const ModuleDecl *module) const {
21472141

21482142
void SourceFile::lookupImportedSPIGroups(const ModuleDecl *importedModule,
21492143
SmallVectorImpl<Identifier> &spiGroups) const {
2150-
for (auto &import : Imports) {
2144+
for (auto &import : *Imports) {
21512145
if (import.importOptions.contains(ImportFlags::SPIAccessControl) &&
21522146
importedModule == std::get<ModuleDecl*>(import.module)) {
21532147
auto importedSpis = import.spiGroups;

lib/Sema/ImportResolution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void swift::performImportResolution(SourceFile &SF) {
288288
for (auto D : SF.getTopLevelDecls())
289289
resolver.visit(D);
290290

291-
SF.addImports(resolver.getFinishedImports());
291+
SF.setImports(resolver.getFinishedImports());
292292

293293
SF.ASTStage = SourceFile::ImportsResolved;
294294
verify(SF);

0 commit comments

Comments
 (0)