Skip to content

Commit 3c52f06

Browse files
elshbeccadax
andcommitted
Update include/swift/AST/Decl.h
Co-authored-by: Becca Royal-Gordon <[email protected]>
1 parent c076617 commit 3c52f06

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

include/swift/AST/Decl.h

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,28 +1169,46 @@ class ImportDecl final : public Decl,
11691169
return static_cast<ImportKind>(Bits.ImportDecl.ImportKind);
11701170
}
11711171

1172-
/// Retrieves the full import path.
1173-
/// \param outRealModuleName An ImportPath builder to write the real module name to if module aliasing
1174-
/// was used. For example, if '-module-alias Foo=Bar' was passed, the real name 'Bar' will be written
1175-
/// to it, which corresponds to 'import Foo' in source file.
1176-
/// \returns An ImportPath corresponding to this import decl. If module aliasing was used, 'Foo' will be in the
1177-
/// returned path instead of 'Bar' using the above example.
1178-
ImportPath getImportPath(ImportPath::Builder *outRealModuleName = nullptr) const {
1179-
auto path = ImportPath({ getTrailingObjects<ImportPath::Element>(),
1180-
static_cast<size_t>(Bits.ImportDecl.NumPathElements) });;
1181-
1182-
if (outRealModuleName != nullptr && !RealModuleName.empty()) {
1183-
for (auto elem: path) {
1184-
if (outRealModuleName->empty()) {
1185-
// Add the real module name instead of its alias
1186-
outRealModuleName->push_back(RealModuleName);
1187-
} else {
1188-
// Add the rest if any (access path elements)
1189-
outRealModuleName->push_back(elem.Item);
1190-
}
1172+
/// Retrieves the import path as written in the source code.
1173+
///
1174+
/// \returns An \c ImportPath corresponding to this import decl. If module
1175+
/// aliasing was used, this will contain the aliased name of the module;
1176+
/// for instance, if you wrote 'import Foo' but passed
1177+
/// '-module-alias Foo=Bar', this import path will include 'Foo'. This
1178+
/// return value is always owned by the AST context, so it can be
1179+
/// persisted.
1180+
ImportPath getImportPath() const {
1181+
return ImportPath({ getTrailingObjects<ImportPath::Element>(),
1182+
static_cast<size_t>(Bits.ImportDecl.NumPathElements) });
1183+
}
1184+
1185+
/// Retrieves the import path, replacing any module aliases with real names.
1186+
///
1187+
/// \param scratch An \c ImportPath::Builder which may, if necessary, be used to
1188+
/// construct the return value. It may go unused, so you should not try to
1189+
/// read the result from it; use the return value instead.
1190+
/// \returns An \c ImportPath corresponding to this import decl. If module
1191+
/// aliasing was used, this will contain the real name of the module;
1192+
/// for instance, if you wrote 'import Foo' but passed
1193+
/// '-module-alias Foo=Bar', this import path will include 'Bar'. This
1194+
/// return value may be owned by \p scratch, so it should not be used
1195+
/// after \p scratch is destroyed.
1196+
ImportPath getRealImportPath(ImportPath::Builder &scratch) const {
1197+
assert(scratch.empty() && "non-empty scratch ImportPath::Builder?");
1198+
auto path = getImportPath();
1199+
if (RealModuleName.empty())
1200+
return path;
1201+
1202+
for (auto elem : path) {
1203+
if (scratch.empty()) {
1204+
// Add the real module name instead of its alias
1205+
scratch.push_back(RealModuleName);
1206+
} else {
1207+
// Add the rest if any (access path elements)
1208+
scratch.push_back(elem.Item);
11911209
}
11921210
}
1193-
return path;
1211+
return scratch.get();
11941212
}
11951213

11961214
ImportPath::Module getModulePath(ImportPath::Module::Builder *outRealModuleName = nullptr) const {

0 commit comments

Comments
 (0)