Skip to content

Commit 7bfeeeb

Browse files
committed
[NFC] Add helper to parse import paths in strings
1 parent 61f716d commit 7bfeeeb

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

include/swift/AST/Import.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/Identifier.h"
2323
#include "swift/Basic/Located.h"
2424
#include "llvm/ADT/ArrayRef.h"
25+
#include "llvm/ADT/StringRef.h"
2526
#include "llvm/ADT/SmallVector.h"
2627
#include "llvm/ADT/STLExtras.h"
2728

@@ -108,8 +109,11 @@ namespace detail {
108109
}
109110
};
110111

112+
// These shims avoid circularity between ASTContext.h and Import.h.
111113
ImportPathRaw ImportPathBuilder_copyToImpl(ASTContext &ctx,
112114
ImportPathRaw raw);
115+
Identifier ImportPathBuilder_getIdentifierImpl(ASTContext &ctx,
116+
StringRef string);
113117

114118
template<typename Subclass>
115119
class ImportPathBuilder {
@@ -138,6 +142,21 @@ namespace detail {
138142
ImportPathBuilder(Range collection)
139143
: scratch(collection.begin(), collection.end()) { }
140144

145+
/// Parses \p text into elements separated by \p separator, with identifiers
146+
/// from \p ctx and invalid SourceLocs.
147+
///
148+
/// \warning This is not very robust; for instance, it doesn't check the
149+
/// validity of the identifiers.
150+
ImportPathBuilder(ASTContext &ctx, StringRef text, char separator)
151+
: scratch()
152+
{
153+
while (!text.empty()) {
154+
StringRef next;
155+
std::tie(next, text) = text.split(separator);
156+
push_back(ImportPathBuilder_getIdentifierImpl(ctx, next));
157+
}
158+
}
159+
141160
void push_back(const ImportPathElement &elem) { scratch.push_back(elem); }
142161
void push_back(Identifier name, SourceLoc loc = SourceLoc()) {
143162
scratch.push_back({ name, loc });

lib/AST/ASTContext.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,12 @@ swift::detail::ImportPathBuilder_copyToImpl(ASTContext &ctx,
654654
return ctx.AllocateCopy(raw);
655655
}
656656

657+
Identifier
658+
swift::detail::ImportPathBuilder_getIdentifierImpl(ASTContext &ctx,
659+
StringRef string) {
660+
return ctx.getIdentifier(string);
661+
}
662+
657663
/// Set a new stats reporter.
658664
void ASTContext::setStatsReporter(UnifiedStatsReporter *stats) {
659665
if (stats) {
@@ -1911,12 +1917,7 @@ ASTContext::getModule(ImportPath::Module ModulePath) {
19111917
}
19121918

19131919
ModuleDecl *ASTContext::getModuleByName(StringRef ModuleName) {
1914-
ImportPath::Module::Builder builder;
1915-
while (!ModuleName.empty()) {
1916-
StringRef SubModuleName;
1917-
std::tie(SubModuleName, ModuleName) = ModuleName.split('.');
1918-
builder.push_back(getIdentifier(SubModuleName));
1919-
}
1920+
ImportPath::Module::Builder builder(*this, ModuleName, /*separator=*/'.');
19201921
return getModule(builder.get());
19211922
}
19221923

lib/Serialization/ModuleFile.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,12 @@ Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc) {
196196
modulePathStr = modulePathStr.slice(0, splitPoint);
197197
}
198198

199-
ImportPath::Module::Builder modulePath;
200-
while (!modulePathStr.empty()) {
201-
StringRef nextComponent;
202-
std::tie(nextComponent, modulePathStr) = modulePathStr.split('\0');
203-
modulePath.push_back(ctx.getIdentifier(nextComponent));
204-
assert(!modulePath.back().Item.empty() &&
199+
// TODO: Further simplification is possible by unifying scopePath above with
200+
// this.
201+
ImportPath::Module::Builder modulePath(ctx, modulePathStr,
202+
/*separator=*/'\0');
203+
for (const auto &elem : modulePath) {
204+
assert(!elem.Item.empty() &&
205205
"invalid module name (submodules not yet supported)");
206206
}
207207
auto module = getModule(modulePath.get(), /*allowLoading*/true);
@@ -474,14 +474,7 @@ void ModuleFile::getImportDecls(SmallVectorImpl<Decl *> &Results) {
474474
if (Dep.isHeader())
475475
continue;
476476

477-
ImportPath::Builder importPath;
478-
479-
StringRef ModulePathStr = Dep.Core.RawPath;
480-
while (!ModulePathStr.empty()) {
481-
StringRef NextComponent;
482-
std::tie(NextComponent, ModulePathStr) = ModulePathStr.split('\0');
483-
importPath.push_back(Ctx.getIdentifier(NextComponent));
484-
}
477+
ImportPath::Builder importPath(Ctx, Dep.Core.RawPath, /*separator=*/'\0');
485478

486479
if (importPath.size() == 1
487480
&& importPath.front().Item == Ctx.StdlibModuleName)

0 commit comments

Comments
 (0)