Remove FailedImportModule. A failed import is now represented as an empty TU.

No other functionality change.

Swift SVN r10833
This commit is contained in:
Jordan Rose
2013-12-05 01:51:10 +00:00
parent 8b8cc8ee62
commit 68272b15c4
3 changed files with 21 additions and 60 deletions

View File

@@ -634,6 +634,8 @@ class TranslationUnit : public Module {
/// lookups. /// lookups.
ExternalNameLookup *ExternalLookup = nullptr; ExternalNameLookup *ExternalLookup = nullptr;
// FIXME: This storage is never freed, because Modules are allocated on the
// ASTContext.
TinyPtrVector<FileUnit *> Files; TinyPtrVector<FileUnit *> Files;
public: public:
@@ -657,6 +659,18 @@ public:
Files.push_back(&newFile); Files.push_back(&newFile);
} }
void removeFile(FileUnit &existingFile) {
// Do a reverse search; usually the file to be deleted will be at the end.
std::reverse_iterator<decltype(Files)::iterator> I(Files.end()),
E(Files.begin());
I = std::find(I, E, &existingFile);
assert(I != E);
// Adjust for the std::reverse_iterator offset.
++I;
Files.erase(I.base());
}
/// Convenience accessor for clients that know what kind of file they're /// Convenience accessor for clients that know what kind of file they're
/// dealing with. /// dealing with.
SourceFile &getMainSourceFile(SourceFile::SourceKind expectedKind) const { SourceFile &getMainSourceFile(SourceFile::SourceKind expectedKind) const {

View File

@@ -27,20 +27,6 @@ class ModuleFile;
/// \brief Imports serialized Swift modules into an ASTContext. /// \brief Imports serialized Swift modules into an ASTContext.
class SerializedModuleLoader : public ModuleLoader { class SerializedModuleLoader : public ModuleLoader {
private: private:
/// This is only used to pass as owner of FailedImportModules so that the
/// rest of SerializedModuleLoader can assume that it is receiving valid
/// modules.
class FailedImportModuleLoader : public ModuleLoader {
Module *loadModule(SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path) override {
return nullptr;
}
StringRef getModuleFilename(const Module *Module) override;
};
FailedImportModuleLoader FailedImportLoader;
ASTContext &Ctx; ASTContext &Ctx;
std::map<std::string, std::unique_ptr<llvm::MemoryBuffer> > MemoryBuffers; std::map<std::string, std::unique_ptr<llvm::MemoryBuffer> > MemoryBuffers;
/// A { module, generation # } pair. /// A { module, generation # } pair.
@@ -108,28 +94,6 @@ enum class ModuleStatus {
Malformed Malformed
}; };
/// Represents a module that failed to get imported.
class FailedImportModule : public LoadedModule {
public:
const ModuleStatus Status;
std::string ModuleFilename;
FailedImportModule(Identifier Name, ModuleStatus Status,
StringRef ModuleFilename,
ASTContext &Ctx, ModuleLoader &Owner)
: LoadedModule(ModuleKind::FailedImport, Name, {}, Ctx, Owner),
Status(Status) {
assert(Status != ModuleStatus::Valid && "module is valid ?");
}
static bool classof(const Module *M) {
return M->getKind() == ModuleKind::FailedImport;
}
static bool classof(const DeclContext *DC) {
return isa<Module>(DC) && classof(cast<Module>(DC));
}
};
} // end namespace swift } // end namespace swift
#endif #endif

View File

@@ -33,11 +33,6 @@ typedef std::pair<Identifier, SourceLoc> AccessPathElem;
SerializedModuleLoader::SerializedModuleLoader(ASTContext &ctx) : Ctx(ctx) {} SerializedModuleLoader::SerializedModuleLoader(ASTContext &ctx) : Ctx(ctx) {}
SerializedModuleLoader::~SerializedModuleLoader() = default; SerializedModuleLoader::~SerializedModuleLoader() = default;
StringRef SerializedModuleLoader::FailedImportModuleLoader::getModuleFilename(
const Module *Module) {
return cast<FailedImportModule>(Module)->ModuleFilename;
}
// FIXME: Copied from SourceLoader. Not bothering to fix until we decide that // FIXME: Copied from SourceLoader. Not bothering to fix until we decide that
// the source loader search path should be the same as the module loader search // the source loader search path should be the same as the module loader search
// path. // path.
@@ -139,21 +134,14 @@ Module *SerializedModuleLoader::loadModule(SourceLoc importLoc,
} }
// Whether we succeed or fail, don't try to load this module again. // Whether we succeed or fail, don't try to load this module again.
Module *&moduleRef = Ctx.LoadedModules[moduleID.first.str()];
if (err != ModuleStatus::Valid) {
StringRef name = loadedModuleFile->getModuleFilename();
moduleRef = new (Ctx) FailedImportModule(moduleID.first, err, name,
Ctx, FailedImportLoader);
return moduleRef;
}
auto TU = new (Ctx) TranslationUnit(moduleID.first, Ctx); auto TU = new (Ctx) TranslationUnit(moduleID.first, Ctx);
Ctx.LoadedModules[moduleID.first.str()] = TU;
if (err != ModuleStatus::Valid)
return TU;
auto fileUnit = new (Ctx) SerializedASTFile(*TU, *loadedModuleFile); auto fileUnit = new (Ctx) SerializedASTFile(*TU, *loadedModuleFile);
TU->addFile(*fileUnit); TU->addFile(*fileUnit);
moduleRef = TU;
// moduleRef is no longer valid as soon as we start loading dependencies.
if (loadedModuleFile->associateWithFileContext(fileUnit)) { if (loadedModuleFile->associateWithFileContext(fileUnit)) {
LoadedModuleFiles.emplace_back(std::move(loadedModuleFile), LoadedModuleFiles.emplace_back(std::move(loadedModuleFile),
@@ -162,6 +150,7 @@ Module *SerializedModuleLoader::loadModule(SourceLoc importLoc,
} }
// We failed to bring the module file into the AST. // We failed to bring the module file into the AST.
TU->removeFile(*fileUnit);
assert(loadedModuleFile->getStatus() == ModuleStatus::MissingDependency); assert(loadedModuleFile->getStatus() == ModuleStatus::MissingDependency);
SmallVector<ModuleFile::Dependency, 4> missing; SmallVector<ModuleFile::Dependency, 4> missing;
@@ -194,13 +183,7 @@ Module *SerializedModuleLoader::loadModule(SourceLoc importLoc,
} }
// Don't try to load this module again. // Don't try to load this module again.
Module *failedModule = return TU;
new (Ctx) FailedImportModule(moduleID.first,
loadedModuleFile->getStatus(),
loadedModuleFile->getModuleFilename(),
Ctx, FailedImportLoader);
Ctx.LoadedModules[moduleID.first.str()] = failedModule;
return failedModule;
} }
void SerializedModuleLoader::loadExtensions(NominalTypeDecl *nominal, void SerializedModuleLoader::loadExtensions(NominalTypeDecl *nominal,