Add ModuleDecl::ReverseFullNameIterator

Package up the logic that generates a full Clang module name, so that
(a) we don't have to deal with clang::Module in quite as many places
in the /Swift/ compiler, and (b) we can avoid the cost of a temporary
string in a few places.

The main places where this is /not/ adopted is where we don't just
want to know the parent module name, but actually the module itself.
This is mostly indexing-related queries, which use the very similar
ModuleEntity class also defined in Module.h. I didn't quite see an
obvious way to unify these, but that might be where we want to go.

No functionality change.
This commit is contained in:
Jordan Rose
2018-09-06 19:47:25 -07:00
parent d6133f408d
commit 37ec248823
7 changed files with 113 additions and 61 deletions

View File

@@ -2654,15 +2654,6 @@ public:
return import == importer->getImportedHeaderModule();
}
static void getClangSubmoduleReversePath(SmallVectorImpl<StringRef> &path,
const clang::Module *module) {
// FIXME: This should be an API on clang::Module.
do {
path.push_back(module->Name);
module = module->Parent;
} while (module);
}
static int compareImportModulesByName(const ImportModuleTy *left,
const ImportModuleTy *right) {
auto *leftSwiftModule = left->dyn_cast<ModuleDecl *>();
@@ -2692,10 +2683,10 @@ public:
assert(rightClangModule->isSubModule() &&
"top-level modules should use a normal swift::ModuleDecl");
SmallVector<StringRef, 8> leftReversePath;
SmallVector<StringRef, 8> rightReversePath;
getClangSubmoduleReversePath(leftReversePath, leftClangModule);
getClangSubmoduleReversePath(rightReversePath, rightClangModule);
SmallVector<StringRef, 8> leftReversePath(
ModuleDecl::ReverseFullNameIterator(leftClangModule), {});
SmallVector<StringRef, 8> rightReversePath(
ModuleDecl::ReverseFullNameIterator(rightClangModule), {});
assert(leftReversePath != rightReversePath &&
"distinct Clang modules should not have the same full name");
@@ -2734,11 +2725,7 @@ public:
assert(clangModule->isSubModule() &&
"top-level modules should use a normal swift::ModuleDecl");
out << "@import ";
SmallVector<StringRef, 8> submoduleNames;
getClangSubmoduleReversePath(submoduleNames, clangModule);
interleave(submoduleNames.rbegin(), submoduleNames.rend(),
[&out](StringRef next) { out << next; },
[&out] { out << "."; });
ModuleDecl::ReverseFullNameIterator(clangModule).printForward(out);
out << ";\n";
}
}